views:

500

answers:

4

Hi i need to make a VectorIterator, so i need to accept a Vector with any type. I am currently trying to define the type as * like so:

var collection:Vector.<*> = new Vector<*>()

But the compiler is complaining that the type "is not a compile time constant". i know a bug exists with the Vector class where the error reporting, reports the wrong type as missing, for example:

var collection:Vector.<Sprite> = new Vector.<Sprite>()

if Sprite was not imported, the compiler would complain that it cannot find the Vector class. I wonder if this is related?

A: 
var collection:Vector.<Object> = new Vector.<Object>()

maybe? But i'm just speculating, haven't tried it.

kajyr
This causes a type casting error. If i create a Vector.<Sprite> and pass it to my Iterator, the compiler complains about casting a Vector.<String> to a Vector.<Object>.
Brian Heylin
Just a hunch - try declaring the method as accepting a Vector (with no type parameters)
Richard Szalay
That is illegal syntax according to the compiler. it at least expects Vector.<*>()
Brian Heylin
A: 
var collection:Vector.<Object> = new Vector.<Object>()

but only on targeting flash player 10 cs4

Shvilam
This causes a type casting error. If i create a Vector.<Sprite> and pass it to my Iterator, the compiler complains about casting a Vector.<String> to a Vector.<Object>
Brian Heylin
+4  A: 

So it looks like the answer is there is no way to implicitly cast a Vector of a type to valid super type. It must be performed explicitly with the global Vector.<> function.

So my actual problem was a mix of problems :)

It is correct to use Vector. as a generic reference to another Vector, but, it cannot be performed like this:

var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = spriteList // this will cause a type casting error

The assignment should be performed using the global Vector() function/cast like so:

var spriteList:Vector.<Sprite> = new Vector.<Sprite>()
var genericList:Vector.<Object> = new Vector.<Object>()
genericList = Vector.<Object>(spriteList)

It was a simple case of me not reading the documentation.

Below is some test code, I would expect the Vector. to cast implicitly to Vector.<*>.

public class VectorTest extends Sprite
{
 public function VectorTest()
 {
        // works, due to <*> being strictly the same type as the collection in VectorContainer
  var collection:Vector.<*> = new Vector.<String>() 

        // compiler complains about implicit conversion of <String> to <*>
        var collection:Vector.<String> = new Vector.<String>()
  collection.push("One")
  collection.push("Two")
  collection.push("Three")

  for each (var eachNumber:String in collection)
  {
   trace("eachNumber: " + eachNumber)
  }

  var vectorContainer:VectorContainer = new VectorContainer(collection)

  while(vectorContainer.hasNext())
  {
   trace(vectorContainer.next) 
  }
 }
}

public class VectorContainer
{
 private var _collection:Vector.<*>

 private var _index:int = 0

 public function VectorContainer(collection:Vector.<*>)
 {
  _collection = collection
 }

 public function hasNext():Boolean
 {
  return _index < _collection.length
 }

 public function get next():*
 {
  return _collection[_index++]
 }
}
Brian Heylin
Here's the iterator constructor VectorIterator(collection:Vector.<Object>)and here is how the creation looks:var spriteList:Vector.<Sprite> = new Vector.<Sprite>()var iterator:VectorIterator = new VectorIterator(Vector.<Object>(spriteList)) pretty ugly :)
Brian Heylin
Does this mean there's no way to retain the typing? I.e. have a VectorIterator.<Sprite> or VectorIterator.<Vector.<Sprite>>...
bzlm
yep, as far as I know. Generics are not a part of the ECMA language spec, so there is no way for me to define a class with a generic type. I can use the Vector class, but that is more of a compiler level hack as far as I can tell.
Brian Heylin
@Brian - Vectors are supported natively by the AVM. I've not looked at how they are defined but how they are referenced is definitely unique. See my self-answered question - http://stackoverflow.com/questions/553445/how-do-generics-vector-work-inside-the-avm
Richard Szalay
Thanks Richard, it's interesting to see how Vectors were implemented, hopefully Generics will become part of the language soon, along with a decent Proxy system for as3mock, make mocking a hell of a lot easier for you :D
Brian Heylin
@Brian - asmock actually already supports dynamic mocking (which would explain research into the AVM) - you can check it out at asmock.sourceforge.net
Richard Szalay
A: 

I believe you can refer to an untyped Vector by just calling it Vector (no .<>)

Richard Szalay
That is illegal syntax according to the compiler. it at least expects Vector.<*>()
Brian Heylin