Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active November 23, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osa1/b17e09f84c33c0d478640e6d7811ed37 to your computer and use it in GitHub Desktop.
Save osa1/b17e09f84c33c0d478640e6d7811ed37 to your computer and use it in GitHub Desktop.
Strange Dart 11
/*
Bad type inference causes breakage when making a type more precise in covariant
position.
*/
class A {
const A();
}
const constA = const A();
A test() => A();
void main() {
var a = test();
if (identical(a, constA)) {
a = A();
}
print(a);
}
/*
Now try to replace `test` with:
```
class B extends A {}
B test() => B();
```
The problem is that the inferred type of `a` becomes `B`, so `a = A()` is now a
type error.
The fix is to give `a` an explicit type:
```
A a = test();
```
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment