You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a tricky issue (and not the first time it has cropped up).
The specification for const maps requires the key objects to not override operator==. The objects are clearly not instances of just Type since that class is abstract. They are instances of some other class that implements Type, and it turns out that this type does override operator== in dart2js.
I agree that the user has no chance of knowing this. The solution used for Symbol was to add an abstract operator== to the abstract class to make it clear.
It might be possible to do better with types, though.
We do not promise to canonicalize all Type objects. There isn't necessarily a finite number of different Type objects during a program's life, so that could leak memory like crazy. It may be possible to canonicalize only the types that can occur as literals (pure class types with no generic parameters), and use a different class for types with type parameters since there is at most one such unparameterized type per class in the language. That would mean that type compile-time constant type objects does not need to override operator==.
The following source code compiles and runs without error on the VM:
import "package:expect/expect.dart";
main() {
const m = const { dynamic: 1, int: 2 };
Expect.equals(1, m[dynamic]);
Expect.equals(2, m[int]);
}
But with dart2js it produces the compile-time error "Const-map key type 'TypeImpl' overrides 'operator =='."
I believe the VM behavior is correct. Since the definition of the class "Type" in the SDK is:
abstract class Type {}
it is reasonable for the user to expect that they can use Type objects as keys in constant maps.
The text was updated successfully, but these errors were encountered: