-
Notifications
You must be signed in to change notification settings - Fork 205
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
Null-aware elements #323
Comments
In the short term, you can nest the
|
@bwilkerson I want to check the value of
|
cc @munificent |
This is an interesting corner. We could do some kind of single-value null-aware element syntax like you suggest. I'm very hesitant to pile more semantics onto In many cases, you can use the other UI-as-code features like Brian suggests, but in this case you want to avoid any redundant computation. Of course, the natural way to do that is with a local variable, which makes me think building the list imperatively like you do today probably is the best approach. |
We could introduce a [ let tmp = something in if (tmp != null) tmp ] which is obviously not as short as [ for (var tmp in [something]) if (tmp != null) tmp ] or [... [something].where(isNotNull)] or (after NNBD) [... [something].whereType<Object>()] All of these will likely introduce an intermediate list, with only the first example being likely to have that optimized away. |
I think the only difference is that in #219, you propose allowing |
Just saw code of the form: List<Property<Foo>> get elements => [
something,
other,
orWhatnot,
andYetOne,
andFinally,
].whereNotNull().toList(); I tried to suggest using a literal with List<Property<Foo>> get elements => [
?something,
?other,
?orWhatnot,
?andYetOne,
?andFinally,
]; That would be awesome! |
Agreed! Null-aware spread has spoiled me. We have a fancy null-aware element, but not a simple one. |
Nullable could be updated to implement the int? a = null;
int? b= 5;
final items = [
...a,
...b
];
// items is [5] |
That's a neat trick! 😃 But I'm afraid it requires every type to implement |
If we had Or if we could iterate based on an extension |
Interesting! Didn't realize that nullables were implemented without a wrapper object. I have always wondered why e.g. |
In case you're curious, I wrote a blog post a while back about the rationale behind that design choice: https://medium.com/dartlang/why-nullable-types-7dd93c28c87a |
...
Some time ago I took a shot at getting dart2js to optimize using a singleton list as a 'let'-expression and it was surprisingly difficult to get anything reasonable. I did manage to remove the allocation, but there were all kinds of weird artifacts in the generated that would not exist with a 'let'-expression. I would say that "being likely to have that optimized away" is too optimistic. |
This should work for maps too, like: final String? seasonId = '1';
final queryParameters = <String, Object>{
'seasonId': ?seasonId, // the '?' could instead go in front of the key, instead of the value
}; If Dart allowed for inferring the key, it would look better: final queryParameters = <String, Object>{
?seasonId,
}; Then again, now that I'm reading it, maybe Dart shouldn't allow for inferring the key, looks weird 😅. |
FWIW patterns help with this - the example in the original post could be written as: var result = [
for (var node in nodes)
if (convert(node) case var converted?)
converted
];
|
I just added an in-progress proposal for this: 329f626 Thanks again for the excellent suggestion @alorenzen! |
Looks great! I know this is a separate issue. but would it be possible to extend this to optional parameters as well? final padding = isMobile ? null : EdgeInsets.all(6);
return Container(
padding: ?padding,
child: Text("This may or may not have padding"),
); |
Argument elements is a different issue. It will likely need/want more than just null-aware arguments. At least |
This is an intractable problem (semantic paradox), it cannot be solved without some restrictions, e.g. prohibiting conditionally omitting positional arguments. In the proposed null-aware operator in collections, (Edited by removing some nuts) |
Admin comment: This is under implementation: feature specification, implementation issue
Null-aware elements offer a shorthand for adding an element to a list, if the element is non-null. Consider this code before the feature:
The causes repetition of
widget.child
, and is verbose. With null-aware elements this becomes:Original issue contents:
I would love to have a "null-aware" operator for adding an element to a list if it is non-null.
This would be equivalent to:
I've been trying out the new "UI-as-code" features in the angular codebase, and find that I can't convert a common pattern to use the new element syntax.
We often iterate over a list, adding elements to a new list when the value is non-null.
It would be pretty nice if I could instead use the new syntax:
The text was updated successfully, but these errors were encountered: