-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
More null-aware operators #27716
Comments
It is a pattern that occurs reasonably often. It's worth considering. "All the good operators are taken!" :) |
Fwiw, we are considering shortcutting all following member calls, when the first That is double convertFromMicro(Int64 value) => value?.toDouble() / _microDollars; It wouldn't work for the direct operator case, though: |
I personally don't think that extending the reach of the It's possible, defining it so that any member called on the result is also ignored (which ends at the point where you use the value itself for assignment or as a function parameter), but I think it will be too confusing.
Still, if we can make it work, it would be great. |
I would also vote against handling this via extending the reach of ?. operator. I believe it would create readability issues and might lead to more problems than it solves. I like the current explicit way of null guarding. For instance, people might get comfortable and start writing formulas like My original thought was to use the bang op in conjunction with ?? which universally means 'not'. Basically, if LHS null then DO NOT eval the RHS.
We are getting dangerously close to emojis :-). |
Also see dart-lang/language#360. |
Or Anyway, this probably doesn't need more data points for motivation, but an inverted |
An "xargs"/"applyTo" like extension function can work, but it means introducing a closure, which is heavyweight in both syntax and execution overhead. It works today, which is awesome compared to all other suggestions so far. 👍 Naming. Is. Hard. (So is punctuation, but at least this avoids using new operators). |
Most likely we can get inlining most of the time. We're calling a static function (extension methods are static functions) so that's ripe for inlining, and then it should be fairly simple to see that the function literal can be inlined too. |
It would be nice to have a shorthand for this:
This is the opposite of
val ?? method(val)
which only returns method(val) when val is null.We love the null friendly operators so it bugs me a lot that I can't do this. Normally you would not need it if method can do ?. everywhere but sometimes the shorthand helps. For instance, some operators are not null friendly:
value?.toDouble() / _microDollars
would blow up if value is null.The text was updated successfully, but these errors were encountered: