Dynamically instantiate a typed Vector from functi

2019-04-29 16:59发布

For a game I'm attempting to develop, I am writing a resource pool class in order to recycle objects without calling the "new" operator. I would like to be able to specify the size of the pool, and I would like it to be strongly typed.

Because of these considerations, I think that a Vector would be my best choice. However, as Vector is a final class, I can't extend it. So, I figured I'd use composition instead of inheritance, in this case.

The problem I'm seeing is this - I want to instantiate the class with two arguments: size and class type, and I'm not sure how to pass a type as an argument.

Here's what I tried:

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        objects = new Vector.<type>(poolsize);  // line 15
    }
}

And here's the error I receive from FlashDevelop when I try to build:

\src\ObjPool.as(15): col: 18 Error: Access of undefined property type.

Does anybody know of a way to do this? It looks like the Flash compiler doesn't like to accept variable names within the Vector bracket notation. (I tried changing constructor parameter "type" to String as a test, with no results; I also tried putting a getQualifiedClassName in there, and that didn't work either. Untyping the objects var was fruitless as well.) Additionally, I'm not even sure if type "Class" is the right way to do this - does anybody know?

Thanks!

Edit: For clarification, I am calling my class like this:

var i:ObjPool = new ObjPool(5000, int);

The intention is to specify a size and a type.

Double Edit: For anyone who stumbles upon this question looking for an answer, please research Generics in the Java programming language. As of the time of this writing, they are not implemented in Actionscript 3. Good luck.

5条回答
看我几分像从前
2楼-- · 2019-04-29 17:20

I have been trying to do this for a while now and Dominic Tancredi's post made me think that even if you can't go :

objects = new Vector.<classType>(poolsize);

You could go something like :

public final class ObjPool
{
    private var objects:Vector.<*>;

    public function ObjPool(poolsize:uint, type:Class)
    {
        var className   : String = getQualifiedClassName(type);
        var vectorClass : Class  = Class(getDefinitionByName("Vector.<" + className + ">"));
        objects = new vectorClass(poolsize);
    }
}

I tried it with both int and a custom class and it seems to be working fine. Of course you would have to check if you actually gain any speed from this since objects is a Vector.<*> and flash might be making some implicit type checks that would negate the speed up you get from using a vector.

Hope this helps

查看更多
对你真心纯属浪费
3楼-- · 2019-04-29 17:23

Shot in the dock, but try this:

var classType:Class = getDefinitionByName(type) as Class;
...
objects = new Vector.<classType>(poolsize);  // line 15

drops the mic

查看更多
女痞
4楼-- · 2019-04-29 17:27

It is good you trying to stay away from new but:

Everything I have ever read about Vector<> in actionscript says it must be strongly typed. So this shouldn't work.

Edit: I am saying it can't be done. Here see if this helps.

Is it possible to define a generic type Vector in Actionsctipt 3?

查看更多
劫难
5楼-- · 2019-04-29 17:30

This is an interesting question (+1!), mostly because I've never tried it before. It seems like from your example it is not possible, which I do find odd, probably something to do with how the compiler works. I question why you would want to do this though. The performance benefit of a Vector over an Array is mostly the result of it being typed, however you are explicitly declaring its type as undefined, which means you've lost the performance gain. So why not just use an array instead? Just food for though.

EDIT

I can confirm this is not possible, its an open bug. See here: http://bugs.adobe.com/jira/browse/ASC-3748 Sorry for the news!

Tyler.

查看更多
beautiful°
6楼-- · 2019-04-29 17:37

I don't really see the point in using a Vector.<*>. Might as well go with Array.

Anyhow, I just came up with this way of dynamically create Vectors:

public function getCopy (ofVector:Object):Object
{
    var copy:Object = new ofVector.constructor;
   // Do whatever you like with the vector as long as you don't need to know the type

    return copy;
}
查看更多
登录 后发表回答