Skip to content

Destructuring nullable type in refutable pattern should promote type to non-null #4580

@benthillerkus

Description

@benthillerkus

Yesterday there was a discussion on the Dart community discord on doing something like this:

class Platform {
  Object? core;
}

late final Platform? _corePlatform;

void main() {
    // Have both the _platform_ and the _core_ bound to a name
    // & checked for being non-null
  if (_corePlatform case Platform(:final Object core)) {
    print("${_corePlatform.core} $core");
  }
}

The example program doesn't type check, because _corePlatform is not promoted from Platform? to Platform, even though Platform(:final Object core)) implies this to be true.

Doing this solves the problem:

  if (_corePlatform case final Platform platform && Platform(:final Object core)) {
    print("${platform.core} $core");
  }

but it would be nice if it wasn't necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions