-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected error when down-casting a List #2087
Comments
From the documentation for
Seems like the docs covered this already, noting that even if you cast your list from Some discussion on this repo seems to indicate that the reason this happens is that class A { }
class B extends A { }
class C extends A { }
void main() {
List<B> b = [B()];
List<A> a1 = b.cast<A>();
List<A> a2 = List.of(b);
a1.add(C()); // runtime error
a2.add(C()); // okay
} |
Hmm, ok. That's really odd, I would expect the error to happen when the cast happens then, not sometime later when something is added. Like why even allow this? What about in the case of a toList()? I guess that is just doing an implicit cast as well? Very sneaky :(
It's just very confusing, cause even if ty for the workaround! |
@esDotDev, if you use the But if you want to use the given list as a void main() {
List<Foo> fl= [Foo()];
List<Mixin> l = List.of(fl);
l.add(Bar()); // Does not throw.
} You could also use |
Thanks for the clarification. This makes sense to me when speaking of a cast on the list itself, but I'm still confused why
We don't use cast in practice (I didn't know it existed until today), we normally use toList() in situations like this, and let the compiler infer type. |
The What you might want is |
Ah, ok, so it basically comes down to the fact the compiler is allowing this: List<Foo> fl= [Foo()];
List<Mixin> l = fl; // This is not really a List<Mixin>, but compiler says nothing. Is there a linter rule to have the compiler call this out for me? In terms of usage, a manual List.of() seems to work fine. It's just that I expected I mean, looing at the function definition it should? List<E> toList({bool growable = true}) {
return List<E>.of(this, growable: growable);
} Why is
|
My understanding is that this will be a language feature when static variance is introduced. Specifically, this looks like a case of use-site variance, #753.
Notice the usage of List<Mixin> l = List.of<Mixin>(fl); // okay, E is Mixin However, when you use List<Mixin> l = fl.toList<Mixin>(); // toList is declared with 0 type parameters So List<Mixin> l = fl.map<Mixin>((i) => i as Mixin).toList(); Now, |
Ok, ty all for the time! |
This code will throw the error:
Uncaught Error: TypeError: Instance of 'Bar': type 'Bar' is not a subtype of type 'Foo'
If you rewrite it as this, it works as expected:
If you replace the
cast
(ortoList()
) with amap().toList()
, it will also work as expected:This is not specific to mixins, class + extends has identical behavior.
The text was updated successfully, but these errors were encountered: