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

前端 未结 6 2062
遇见更好的自我
遇见更好的自我 2021-02-08 22:04

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.<         


        
相关标签:
6条回答
  • 2021-02-08 22:28

    With Apache Flex 4.11.0, you can already do what you want. It might have been there since 4.9.0, but I have not tried that before.

    0 讨论(0)
  • 2021-02-08 22:29

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

    0 讨论(0)
  • 2021-02-08 22:37
    [Bindable]
    public var selectedItems:Vector.<Category>;
    
    public function selectionChange(items:Vector.<Object>):void
    {
        selectedItems = Vector.<Category>(items);
    }
    
    0 讨论(0)
  • 2021-02-08 22:45
    var collection:Vector.<Object> = new Vector.<Object>()
    

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

    0 讨论(0)
  • 2021-02-08 22:46
    var collection:Vector.<Object> = new Vector.<Object>()
    

    but only on targeting flash player 10 cs4

    0 讨论(0)
  • 2021-02-08 22:50

    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++]
        }
    }
    
    0 讨论(0)
提交回复
热议问题