Skip to content

Promote types within record fields in pattern matches #3160

Closed as not planned
@nex3

Description

@nex3

Consider the following code:

void function(int? arg1, int? arg2) {
  switch ((arg1, arg2)) {
    case (null, null): print("both null");
    case (var first, null): print(first.abs());
    case (null, var second): print(second.abs());
  }
}

To a human, it's clear that first and second will never be null because of the preceding (null, null) pattern. However, Dart currently doesn't promote their types. I would like it to do so.

The lack of this feature particularly makes it harder to what @munificent describes as "a really common, important use case for patterns [of] doing "parallel" matching where you switch over an immediately-created record" in the presence of nullable types.

(Relatedly, it would also be nice if arg1 and arg2 were promoted to non-nullable after the switch statement.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problemstype-inferenceType inference, issues or improvements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions