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

2

6

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?

Snocat answered 23/3, 2011 at 9:23 Comment(0)
D
1

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;
        }
    }
}
Dunne answered 23/3, 2011 at 12:33 Comment(0)
V
0

You can't do that, so I would go with your suggestion, which is to create a special class containing two properties (say Number, String) and create a Vector of that.

Vershen answered 23/3, 2011 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.