Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 771
BefittingTheorem Avatar asked Feb 28 '09 17:02

BefittingTheorem


2 Answers

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++]
    }
}
like image 84
BefittingTheorem Avatar answered Oct 21 '22 21:10

BefittingTheorem


[Bindable]
public var selectedItems:Vector.<Category>;

public function selectionChange(items:Vector.<Object>):void
{
    selectedItems = Vector.<Category>(items);
}
like image 1
martin Avatar answered Oct 21 '22 22:10

martin