If I have a method like this:
private function operateOnVector(v:Vector.<Object>):void
and I pass that method (at runtime) something that isn't Vector.<Object>, I get the following error. Even if that type extends the type of the vector.
TypeError: Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<com.example::SomeClass>@1a1de6a1 to __AS3__.vec.Vector.<Object>
But, if I change the method so that it takes Vector.<*> like this:
private function operateOnVector(v:Vector.<*>):void
Then it works fine at runtime.
All of the above was done by passing an untyped variable to the function which happened to be a certain type of vector. But if I wanted to take advantage of the compile time type checking I get errors like this:
1067: Implicit coercion of a value of type __AS3__.vec:Vector.<com.example:SomeClass> to an unrelated type __AS3__.vec:Vector.<*>.
Even though that is OK at runtime.
So, the question is: Why is there a compile time error for passing a 'typed vector' (i.e. Vector.<SomeClass>) to a method that takes an 'untyped vector' (i.e. Vector.<*>) when it works fine at runtime?
(EDIT: Yes, I know about the global Vector function: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/package.html#Vector())
What is SomeClass, extends an Object??
Actually SomeClass here cannot be accepted as an Object.
Instead you may try to type-cast is using the "as" keyword.
Vector.<SomeClass as Object>...something like this
Though in this case I am not sure if it will work for a Vector collection. Try and let me know
Thank you for the reply.
Yes, the class inherits from Object.
I tried using the "as" keyword but I got the following compile error:
1084: Syntax error: expecting greaterthan before as.
I tried each of these individually:
trace(""+Vector.<SomeClass as Object>()); var y:Vector.<SomeClass as Object> = null; var x:* = new Vector.<SomeClass as Object>();
I think you are experiencing a limitation in the Vector class. Feel free to
file a bug. Vector. is.