Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically instantiate a typed Vector from function argument?

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.

like image 987
jedd.ahyoung Avatar asked Apr 01 '11 02:04

jedd.ahyoung


People also ask

Can vectors grow dynamically?

Vector are implemented as dynamic arrays with list interface whereas arrays can be implemented as statically or dynamically with primitive data type interface. Size of arrays are fixed whereas the vectors are resizable i.e they can grow and shrink as vectors are allocated on heap memory.

How do you return a vector from a function in C++?

Return by Value It is a preferred method to return vector from function in C++. It can reduce the execution time and space because in this method, an object is not copied for returning a vector. It only points the pointer to the vector which will be returned.


2 Answers

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.

like image 195
Tyler Egeto Avatar answered Nov 15 '22 04:11

Tyler Egeto


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?

like image 38
Feltope Avatar answered Nov 15 '22 05:11

Feltope