Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strongly typed collection with multiple base types in ActionScript (Vector <T,T>)?

Does ActionScript have any way of handling a strongly typed list with multiple base types?

I am actually looking for something such as a Vector<T,T> ?

Is it possible?

Or is the only way of doing it is creating my own class which accepts lets say a String and Number in the constructor and create a Vector<T> out of that class?

like image 855
Ranhiru Jude Cooray Avatar asked Mar 23 '11 09:03

Ranhiru Jude Cooray


1 Answers

No, not by standard. If the items are not one of the primative types you can build a Vector of interfaces, or super classes. For example, a vector of DisplayObjects that contain a mixture of MovieClips and Sprites (which both inherit from the DisplayObject).

For example:

var v:Vector.<DisplayObject> = new <DisplayObject>[
  new MovieClip(), 
  new Sprite(), 
  new MovieClip()
];

trace(v[0].alpha); // outputs 1
trace(v[0].currentFrame); // error - not a DisplayObject property

In this case the vectors item will only expose the properties and methods of itself that stem from the Vectors type. But this is exactly the reason you should use vectors, it ensures the items type you are handling.

I don't know your specific case or goal, but I would consider why you need a mixed type within a vector. Your alternative option, as you stated, would be to create a wrapper class. The example below is far from complete but a starting point.

class Wrapper {
    public var _value:*; // should be private with get/set's

    public function Wrapper(value:*) {
        if(value is String || value is Number) {
            _value = value;
        }
    }
}
like image 170
Chris Avatar answered Oct 05 '22 22:10

Chris