To loop back on this: I don't think any behavior has changed in dart2js or
the VM, but Alexandre added new checks as of Dart ~2.0.0-dev.32.0. Thanks
Alexandre!
- NULL_AWARE_BEFORE_OPERATOR: "The left operand uses '?.', so its value
can be null."
For example, in `foo?.length + 1`
- NULL_AWARE_IN_LOGICAL_OPERATOR: "The value of the '?.' operator can be
'null', which isn't appropriate as an operand of a logical operator."
For example in `foo?.isNotEmpty && bar?.isNotEmpty` or `!foo?.isNotEmpty`
When fixing up code that now features this new Hint, I found a lot of `a &&
b?.c ?? false`. Does this do what you think? Maybe not! It's actually `(a
&& b?.c) ?? false`. Boo. Luckily both the new analyzer hints catch this,
and dartfmt can highlight this when it is forced to wrap lines. For
example, dartfmt will output the following:
void main() {
var result1 =
something.attribute.otherAttribute && somethingOtherThing?.attr?.attr
??
false;
var result2 = something.attribute.otherAttribute &&
(somethingOtherThing?.attr?.attr ?? false);
}
Notice how dartfmt grouped `a && b` onto one line, breaking after `??`. A
small hint. When you change the expressions with parens, for result2,
dartfmt will now keep the `a ?? false` on one line, as that is one
expression.
On Fri, Jun 16, 2017 at 3:53 AM, 'Erik Ernst' via Dart Misc <
Post by 'Erik Ernst' via Dart MiscOn Fri, Jun 16, 2017 at 11:54 AM, Alexandre Ardhuin <
Post by Alexandre ArdhuinWhen Dart expects a boolean value, only the value true is treated as
true.
Couldn't it be possible to make the if statement treat null as false ?
Thus we could simply write `if (foo?.bar) {...}`
We did discuss doing exactly that in general for boolean-controlled
statements, but (if it ever gets enough support to go anywhere) it would be
separate from the normal statements. The conceptual justification would be
that certain things should be done or repeated if "something exists, and it
has a certain property", in which case it makes sense to say that `null`
anywhere in an computation with several steps means "that it doesn't exist"
and then we don't do it. That makes `null` falsey. But I'm pretty sure that
it is never going to _replace_ the current semantics where `null` is an
error.
Alexandre
Post by Alexandre Ardhuin2017-06-16 7:27 GMT+02:00 'Lasse R.H. Nielsen' via Dart Misc <
On Fri, Jun 16, 2017 at 5:03 AM, 'Karan Sikka' via Dart Misc <
Post by 'Karan Sikka' via Dart MiscI agree with all these opinions. Thanks! Will probably use == true. At
least I wont double-take every time I see it.
... but I will when I review your code. I'll think "can't you just
remove that?". Then I'll spend more time examining the code until I realize
that "oh, it's because it might be null as well". At that time, I've wasted
more time than you would have writing it in a different way.
My vote, if I get to vote, is firmly *against* "== true". As stated
earler, it *looks* wrong.
- Never compare to a boolean.
- Never have a boolean literal as either branch of a conditional expression.
If you violate the former, I will read your code to figure out why. If
you violate the latter, I can accept it, but I'll try to rewrite it using
&& or ||.
I'm with Sam Rawlins on this one - his code is longer, but it is
immediately readable.
The ?. operator works fine for staying inside nullable types, and ?? is
a way to get out of the nullability, so
foo?.bar ?? false
works for me on he type level, but ... it's just not as easily readable as
foo != null && foo.bar
/L
Post by 'Karan Sikka' via Dart MiscOn Thu, Jun 15, 2017 at 7:20 PM, 'Samuel Rawlins' via Dart Misc <
Post by 'Samuel Rawlins' via Dart MiscUgh, yeah I hate getting into this situation. It's just a personal
preference, but I often assign straight up avoid null-aware in this
if (foo != null && foo.bar) { ... }
I personally don't like the `if (foo?.bar ?? false)` because it has
the worst ergonomics. It reasons fine, but it just looks like a bizarre
incantation. I think its a failing of null-aware when you're forced to use
two variations at one time just to use it in a conditional.
On Wed, Jun 14, 2017 at 11:02 AM, 'Karan Sikka' via Dart Misc <
Post by 'Karan Sikka' via Dart Miscif (foo?.bar) {...}
You get an error since "foo?.bar" can be null. There are two ways
if (foo?.bar == true) {...}
and
if (foo?.bar ?? false) {...}
Which of these is preferred? (Assuming foo.bar return a bool). Thanks!
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it,
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
--
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 KÞbenhavn K
- Denmark - CVR nr. 28 86 69 84
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
--
Erik Ernst - Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 KÞbenhavn K, Denmark
CVR no. 28866984
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.