Skip to content

! should partipicate in null shorting #1163

Closed
@stereotype441

Description

@stereotype441

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 is null or x.foo is non-null. Users should be able to think that ?. can be replaced by !. when they know that the value will not be null.

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 when x is null, and throws when x is non-null but its foo 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    nnbdNNBD related issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions