dart - Why can I assign List<dynamic> to a List<String>? -
i getting confused around how type checking works dart. shown, assigning general list<dynamic>
list<string>
ok. means can assign content in list, not string
. why that?
void main() { list<string> a; = [1]; // pass = new list<int>(); // fail = 1; // fail = new list<string>(); // pass a.add(1); // fail }
the dynamic
type special. means "turn off type checking this, know i'm doing".
in example, assign list<dynamic>
instance list<string>
variable. static type checker sees: list list, that's ok, , type parameter dynamic, i'll not check @ all, programmer must know doing.
whenever use dynamic
type, or part of type, taking full responsibility typing being correct. system let whatever want.
even without dynamic
, dart type system isn't safe. means can create programs no static type warnings still fail type error @ runtime. languages have problem, actually, contain parameterized types either co- or contra-variant subtyping. or casts.
Comments
Post a Comment