3

I want to create a class that will mainly house a Vector. The class will have some methods that deal with items in the Vector.

The issue I am having at the moment is that I can't work out how to dynamically create an instance of Vector. So far I've tried this and similar with no luck:

public class List
{
    private var _content:Vector;

    public function List(type:Class)
    {
        _content = new Vector.<type>();
    }
}
Andrew Odri
  • 8,868
  • 5
  • 46
  • 55
Marty
  • 39,033
  • 19
  • 93
  • 162

5 Answers5

6

Here is how I dynamically construct a vector of BitmapData (which is required by the MouseCursorData class):

var vectorClassOfBitmapData:Class = Class(getDefinitionByName("__AS3__.vec::Vector.<flash.display::BitmapData>"));

var bitmapDataVector:* = new vectorClassOfBitmapData(1,true);

The above is the same as the compile-time:

var bitmapDataVector:* = new Vector.<BitmapData>(1, true);

In this way, you can compose the class definition string at runtime and use getDefinitionByName to dynamically construct vectors of different data types.

Not exactly what you were after, but it might help others.

5

This post by Paul Robertson (previously Senior ActionScript Developer/Writer at Adobe) provides a little more information on how Vectors are declared:

The Vector class allows (requires) you to specify the type it will contain at compile time — both for variable declarations and when creating instances.

Because the type parameter is a literal, it must be provided at compile time. In fact, every reference to a Vector is checked at compile time, with the exception of .shift() and .unshift, which are checked at run time.

Adobe's article on indexed arrays provides some more interesting information on that. In fact, it mentions that strict compile time type safety is one of the key features of Vectors.

In short: It is not possible to use a variable to set a Vector's type, because the type parameter is a literal and a compile time requirement.

Hope that helps!

Additional References:

Community
  • 1
  • 1
Andrew Odri
  • 8,868
  • 5
  • 46
  • 55
  • No sweat, glad it was helpful; Paul Robertson wrote all of Adobe's Vector documentation, so he's a pretty good resource for this stuff :) – Andrew Odri Dec 29 '11 at 03:57
  • "In short: It is not possible to use a variable to set a Vector's type, because the type parameter is a literal and a compile time requirement." — rubbish. Matthew Peterson answered correctly below. – average dev Jan 13 '12 at 23:25
  • Shame that this is marked as the answer, as it isn't; Matthew Peterson's solution below works. – alecmce Jul 10 '12 at 15:21
4

Another option that might work for you is to use an interface; Vectors do not have to be concrete types. So if you can abstract out some common contract that your objects can abide by, then use that instead. For example, say you wanted a list of renderable objects, you could say:

public interface IRenderable {
    function renderTo(obj:DisplayObject):void;
}

var _content:Vector.<IRenderable> = new Vector.<IRenderable>();

Then you can shove as many different concrete types into the Vector, as long as they implement the IRenderable interface. So while generics in ActionScript 3 are really just syntactic compiler sugar, like Andrew Odri said, you might be able to get around that depending on what you are specifically trying to do.

J. Holmes
  • 18,466
  • 5
  • 47
  • 52
1

Sounds like you just need an Array! The performance is only improved with a Vector<> because the type is sorted out at compile time. If you want a "dynamic" type, then you should use an Array.

Adam Harte
  • 10,369
  • 7
  • 52
  • 85
  • You'd think so, but the performance difference between the two is too vast in a game environment. – Marty Dec 29 '11 at 03:54
0

The original question is a couple years old, but I felt like sharing this because it might help others. It's inspired upon Matthew Peterson's answer but it assumes a little less about the internal class names (it only assumes the .<> syntax).

    function CreateVectorOf(subtype:Class)
    {
        var vecname:String = getQualifiedClassName(Vector);
        var subname:String = getQualifiedClassName(subtype);
        var vecclass:Class = getDefinitionByName(vecname + ".<" + subname + ">") as Class;
        return new vecclass();
    }
royalstream
  • 143
  • 7