Description
This issue captures discusison that has been happening in dart-lang/sdk#43093.
Quoting @lrhn:
@eernstg We have formally specified null shortening now, right?
I would expect!
to be included in the suffix operators that are shortened, but please check.I definitely do not want
x?.foo?.bar;and
x?.foo!.bar;
to behave differently when
x
isnull
orx.foo
is non-null
. Users should be able to think that?.
can be replaced by!.
when they know that the value will not benull
.
Quoting @eernstg:
Given that
!
is a selector when it is used to check for non-null values, I believe the consistent approach is to treat!
accordingly, by adding a rule along the following lines:If `e` translates to `F` then `e!` translates to: `PASSTHRU[F, fn[x] => x!]`
I believe this would ensure that
x?.foo!.bar
evaluates to null whenx
is null, and throws whenx
is non-null but itsfoo
is null.Also, it seems less useful to let
x?.foo!.bar
mean(x?.foo)!.bar
, because this would imply that we didn't mean?.
in the first place.
Proposed spec update here.