dart-lang / dart-lang/language
Switching on bounded generic types
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
It appears exhaustiveness checking isn't performed when switching on bounded generic types.
```dart
sealed class A {
abstract final String s;
}
class B extends A {
final String s = "B";
}
class C extends A {
final String s = "C";
}
// Error: The type 'Type' is not exhaustively matched by the switch cases since it doesn't match 'Type()'.
// Try adding a wildcard pattern or cases that match 'Type()'.
String getString() => switch(T) {
const (A) => throw UnimplementedError,
const (B) => B().s,
const (C) => C().s,
}
```
By adding a default case to this (which I don't can ever be called) the error goes away and this behaves as expected.
I may have missed the proper way to do this. If I had an object of type T then I understand I could use the usual exhaustiveness checking (Matching on B _ etc).
As an aside, matching const nullable types seems to require typedef'ing them, e.g.
```dart
typedef AorNull = A?;
switch(T) {
const (A?) => , // Error: Expected an identifier
const (AorNull) => , // Analyser is happy
}
```
Thanks for your help 🙂
Contributor guide
Research direction
Start by reproducing the bounded-generic switch and nullable-type examples in a Dart analyzer or compiler environment, then read the language specification sections covering switch exhaustiveness, type literals, and constant patterns. Done means determining whether the behavior is a language or analyzer bug and documenting the required resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100