Skip to content

Legacy mode nullability and exhaustiveness checking #2178

Closed
@munificent

Description

@munificent

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    patternsIssues related to pattern matching.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions