I have something like:
class A { }
class B extends A { }
Then I’m trying to cast an instance of A to B:
var a:A = obtainAInstance();
var b:B = B(a);
To this, I get the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert A to B.
What could the cause of this be?
Your
aobject is anAinstance, but sinceAis the parent class rather thanB, it cannot be aBinstance. Thus, you can’t perform such a cast.If your function returns a
Binstance, you can castaback to aB, and access all itsBmembers, because the object is in fact aB: