Search code examples
actionscript-3vector

As3 Vector<T> parametric TYPE


I have one vector instance and I'm exporting swf with Flash Player 10/10.1.

I want initialise it with a parametric type. I tried as follow:

var someType:Class = MyCustomClass;
var v:Vector.<someType> = new Vector.<someType>();

But it doesn't work!! There is a way to do this?

I hope question is clear :-)

Thanks in advance!


Solution

  • someType is the instance of the class type; whereas Vector is a container of that type.

    This should be:

    var v:Vector.<MyCustomClass> = new Vector.<MyCustomClass>();
    

    Otherwise, I've noticed Haxe would compile this as:

    var v:Vector.<Object> = new Vector.<Object>();
    

    Flash polymorphism is lacking, if you had class A and class B, and attempted to push them to a vector of type Class you would receive an error:

    Example

    package
    {
        import flash.display.Sprite;
    
        public class test extends Sprite
        {
            public function test()
            {
                var v:Vector.<Class> = new Vector.<Class>();
    
                var a:A = new A();
                var b:B = new B();
    
                v.push(a);
                v.push(b);
            }
        }
    }
    

    Error:

    TypeError: Error #1034: Type Coercion failed: cannot convert A@43a2ff1 to Class.