Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote types within record fields in pattern matches #3160

Closed
nex3 opened this issue Jun 23, 2023 · 1 comment
Closed

Promote types within record fields in pattern matches #3160

nex3 opened this issue Jun 23, 2023 · 1 comment
Labels
feature Proposed language feature that solves one or more problems type-inference Type inference, issues or improvements

Comments

@nex3
Copy link
Member

nex3 commented Jun 23, 2023

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.)

@nex3 nex3 added the feature Proposed language feature that solves one or more problems label Jun 23, 2023
@lrhn lrhn added the type-inference Type inference, issues or improvements label Jun 26, 2023
@munificent
Copy link
Member

Yeah, I think this is a reasonable request. If I'm reading it right, this is the same request as #2503. I'll close as a duplicate, but feel free to re-open if I got that wrong.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems type-inference Type inference, issues or improvements
Projects
None yet
Development

No branches or pull requests

3 participants