Skip to content

Promote obj to non-null when checking obj?.field == value #4076

Closed
@rrousselGit

Description

@rrousselGit

Consider the following code:

void fn() {
  Class? obj = ...;
  if (obj?.field == 'value') {
    obj.method();
  }
}

Currently, this fails and requires to instead write:

if (obj != null && obj.field == 'value') {
  obj.method();
}

Could we consider obj?.field == <non-nullable expression> to be equivalent to obj != null && obj.field == <expr>?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions