Last active
November 23, 2023 12:29
-
-
Save osa1/b17e09f84c33c0d478640e6d7811ed37 to your computer and use it in GitHub Desktop.
Strange Dart 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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