[5.7] Add whenEmpty + variants to Collection #26345
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I often find myself in need of executing actions with a collection if it is / is not empty. At the moment this is possible in several ways. For example:
Assign the collection to a variable, then reuse the variable in
when()
Pipe the collection and use
if
orwhen()
in there:The first example seems to defeat the purpose of a collection (creating a chain of calls on a collection instance), the second example just seems to be one hell of nested closures to me.
I always add the
whenEmpty()
method (and variants) to my applications as macros. This allows me to fluently do the following and keep chaining:I thought it could prove useful to suggest these shortcuts for implementation. If not I'lll happily continue defining these as macros.