language
language copied to clipboard
Switching on bounded generic types
It appears exhaustiveness checking isn't performed when switching on bounded generic types.
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<T extends A>() => 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.
typedef AorNull = A?;
switch(T) {
const (A?) => , // Error: Expected an identifier
const (AorNull) => , // Analyser is happy
}
Thanks for your help 🙂