Skip to content

Legacy mode nullability and exhaustiveness checking #2178

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

Closed
munificent opened this issue Mar 30, 2022 · 2 comments
Closed

Legacy mode nullability and exhaustiveness checking #2178

munificent opened this issue Mar 30, 2022 · 2 comments
Labels
patterns Issues related to pattern matching.

Comments

@munificent
Copy link
Member

munificent commented Mar 30, 2022

Consider:

// In pre-null-safety code:
main() {
  print(Foo(null));
}

// In modern Dart with pattern support:
class Foo {
  final bool b;
  Foo(this.b);
}

String f(Foo f) {
  switch (f) {
    case Foo(b: true): return 'Foo(true)';
    case Foo(b: false): return 'Foo(false)';
  }
}

This switch isn't actually exhaustive if an instance of Foo can flow in from legacy mode code where b may actually be null. We need to specify how this is handled.

I think the right answer is to have the switch throw a runtime exception if no case matches. The only time this can happen is when legacy code interacts with null safe code. In a fully migrated program, switches can be soundly statically checked for exhaustiveness.

cc @stereotype441

@munificent munificent added the patterns Issues related to pattern matching. label Mar 30, 2022
@stereotype441
Copy link
Member

Note: a similar question came up during the work on null safety (with regard to enum-based switch statements), and we decided that it should throw: #1143 (comment)

@munificent
Copy link
Member Author

Closing in favor of #2178 (which is done).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

2 participants