views:

336

answers:

2

Instead of this:

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

is there any way to do something like this?

var myType:Class = String;
var v:Vector.<myType> = new Vector.<myType>();

Obviously that doesn't work as written, but hopefully you get the idea.

A: 

This is untested, but I can't see why it shouldn't work:

import flash.display.Sprite;
import flash.utils.getDefinitionByName;

var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
var v:Vector.<ClassReference> = new Vector.<ClassReference>();
grapefrukt
Huh? That does precisely the same thing as the non-working example code in my question, except with Sprite instead of String.
fenomas
+3  A: 

Short answer is try grapefrukt's answer and see.

However, I don't think it's possible at a bytecode level. The problem related to how generics (Vectors) are constructed. Basically the bytecode for creating an instance of Vector<> goes:

GenericDefinitionType (Vector) + GenericParameter (int) -> GenericType
Coerce (cast) GenericType as KnownGenericType (eg. "Vector.<int>")

So the issue is not in the creation, since GenericParameter is just a multiname (which can be dynamic). The issue is in the coercion to the known vector type (actually registered as "Vector.<int>" for example) since there is no known vector type.

See my post on how Vectors work in bytecode for the geeky details.

Richard Szalay
Ah, I see what you mean. Thanks!
fenomas