I can't get the same behavior if I use Map literals, neither in the
literal initializer nor in subsequent []= operations. No exception
gets thrown for these two lines:
var m2 = <String, String>{"a": 0}; // Map literal doesn't type-check values
m2[2] = 1; // nor does []= (for keys or values)
Is it possible to type-annotate Map literals and if so, how?
I understand that Dart restricts Map literal keys to Strings and that
those are already checked ("map entry key must be string literal").
/Olov
If we provide type information when instantiating a new Map, we get
runtime type-checking (VM, checked mode):
var m1 = new Map<String, int>();
m1["str"] = "hello"; // error: type OneByteString is not assignable
to type int
m1[1] = 2; // error: type Smi is not assignable to type String
I can't get the same behavior if I use Map literals, neither in the
literal initializer nor in subsequent []= operations. No exception
gets thrown for these two lines:
var m2 = <String, String>{"a": 0}; // Map literal doesn't type-check values
m2[2] = 1; // nor does []= (for keys or values)
Is it possible to type-annotate Map literals and if so, how?
I understand that Dart restricts Map literal keys to Strings and that
those are already checked ("map entry key must be string literal").
Issue 221: <http://code.google.com/p/dart/issues/detail?id=221>
/Olov