-
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
Do collection elements null short? #394
Comments
No, single element expressions should not short-circuit to nothing if they are null, whether or not they contain a I disagree with the reasoning about where we null-short. It's not because we have operators you can use, but because it would be wrong to force you to use those operators. They do not have the same meaning as null-short circuiting. The places where we "null short" is where we know that a We are restricted by grammar because we don't have an explicit null-guard delimiter, and we don't want entire expressions to go away because of a For For For In any case, for Would it work for map literals too: var map = { x?.name : y?.value }; Would this entry be elided if either We do not have a model where What you can do is List<E> ifNotNull<E extends Object>(E? value) => value == null ? const <Null>[] : <E>[value]; |
I agree that the initial motivation for null-shorting was that not null-shorting produces expressions whose evaluation is guaranteed to fail (so we'd have a However, I don't believe that this consideration should be part of the conceptual model for null-shorting: That's about canceling an expression evaluation at a specific point, because a subexpression yielded null ("there was nothing to work on"). This makes the mechanism similar to exception handling, and I think it calls for a very simply (syntactic) criterion for delimiting the parts that we're skipping. So it should be syntactic, we have an explicit marker like If we consider making We could of course make the list literal null-shorting itself, say This would decouple the null-shorting of the list from any null-shorting of each element (so Sets would work just like lists in this respect. Maps could be null-shorting as well. @lrhn already mentioned that it's more messy, but here's a conceptual argument for omitting all bindings that contain just one null: If the value of a key/value pair is null then that key "doesn't map to anything"; if a key of a key/value pair is null then "nothing maps to that value". So we skip a binding as soon as there's at least one null. So It would still make sense to have null-shorting for each element of a set/list as well, cf. #323, especially considering the amount of machinery that @lrhn demonstrated we'd need with |
I don't disagree overall, but I don't think this argument is entirely consistent either:
By that token, we should also null-short both: a?.b + c
a?.b ? c : d Both of those:
In the case of I'm not saying we should short these (I think we probably shouldn't), just that we still don't have a simple explanation for why some things are shorted and these are not.
This would collide with an explicit null-aware element (#323): var list = [
?[null].length
]; Does that produce |
@munificent wrote:
Using a syntactic rule would just imply that the decision is made without reference to context dependent information (like the static type of an expression). So that doesn't tell us anything about whether we should null-short across a Concretely, I suspect that null-shorting across For the ambiguity with |
Reading the comments on this, I don't see a consensus for doing this. I will close this out unless anyone has more they want to say here. |
I think there is consensus that we should not do this, if I'm reading the comments right. |
One of the explanations I've considered for which expressions null short and which don't is "We null short any expression that has a null-aware form". So we null short
.
,[]
, and...
, because we offer?.
,?.[]
, and...?
, respectively.That raises a nasty question if we were to add null-aware elements (#323), which I do think we should. Does that mean single collection elements should null short?
Should this print
[]
, and not[null]
? In other words, do we treat an expression in a collection literal as an "element" that can be implicitly null-shorted if the expression is a null-aware one?It feels pretty dubious to me since there's no syntax in there where the user has indicated they don't want the expression to simply behave like an expression. But it does mean the story that "we short anything that has a null-aware form" is no longer true if we add
?
elements.The text was updated successfully, but these errors were encountered: