-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isEmpty/isNotEmpty for Collections #512
Comments
I agree it's a bit odd that we surface these for Strings but not for other types. In retrospect, I'm still not entirely convinced that surfacing them was the right decision. These functions are mostly useful as quick drop-in utility functions in functional applications like We did discuss something along these lines years ago and ended up on the fence. This came up on the Java side as well in Guava and was rejected, though I believe it ended up accidentally exposed in a helper class at some point and they may be stuck with it now :-) You can see Kevin Bourillion's StackOverflow reply above for rationale as to why it was rejected there. I'm curious to hear a bit about some of the use-cases you can see for this. |
Should extension-methods be used? It's unclear on whether null values are handled. |
So the answer there is that for the moment you can write an extension method that works on null values, but that when Dart moves to non-null by default (NNDB), this will be disallowed in non-nullable contexts. There's some detail in the specification for extension methods. Here's a code snippet that demos this: extension IsNullOrEmpty on List {
bool get isNullOrEmpty {
return this == null || this.isEmpty;
}
}
int main() {
List<int> list;
if (list.isNullOrEmpty)
print('empty');
} Today, this will print "empty". Post-NNBD, this will be disallowed for non-nullable contexts. During migration, it'll be a runtime error for non-nullable contexts. |
For what it's worth, I expect that once developers migrate to non-null by default, a lot of the need for these functions should go away -- same with Quiver's |
Similar to quiver.strings's two useful functions:
Shouldn't there be equivalent methods for collections? For example:
The text was updated successfully, but these errors were encountered: