Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically instantiate a typed Vector from function argument?
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>Here's what I tried:</p> <pre><code>public final class ObjPool { private var objects:Vector.&lt;*&gt;; public function ObjPool(poolsize:uint, type:Class) { objects = new Vector.&lt;type&gt;(poolsize); // line 15 } } </code></pre> <p>And here's the error I receive from FlashDevelop when I try to build:</p> <blockquote> <p>\src\ObjPool.as(15): col: 18 Error: Access of undefined property type.</p> </blockquote> <p>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?</p> <p>Thanks!</p> <p>Edit: For clarification, I am calling my class like this:</p> <pre><code>var i:ObjPool = new ObjPool(5000, int); </code></pre> <p>The intention is to specify a size and a type.</p> <p>Double Edit: For anyone who stumbles upon this question looking for an answer, please research <a href="http://download.oracle.com/javase/tutorial/java/generics/generics.html" rel="nofollow">Generics in the Java programming language</a>. As of the time of this writing, they are not implemented in Actionscript 3. Good luck.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload