The Problem With Vector.<T>
I thought I’d found a bug with respect to Adobe’s new Vector class. I logged it in JIRA, but the answer came back that it is Not A Bug. Respectfully, I disagree. Here’s why.
UPDATE: I may be wrong about this. My brother has written a response: http://bit.ly/9OotIS. For further discussion, please also see the comments below.
The Problem
// create an interface and implementing class
public interface Fruit {}
public class Apple implements Fruit {}
// create a vector of Apple objects and try to cast it to a vector of Fruit objects
public class TheProblemWithVectors
{
public var fruits:Vector.<Fruit>;
public function TheProblemWithVectors()
{
var apples:Vector.<Apple> = new Vector.<Apple>();
apples.push(new Apple());
// THIS THROWS AN ERROR!
fruits = apples as Vector.<Fruit>;
}
}
The following is indisputable:
- All (elements in)
applesmust be anApple; - All
Apples areFruits; - Therefore, (elements in)
applesareFruit.
In which case, why can’t apples be cast as fruits? The Flash player does not accept that a Vector of Fruit could be described as a Vector of Apple. To me this feels like a bug. Does it to you?
Not A Bug?
To Adobe it is Not A Bug:
http://bugs.adobe.com/jira/browse/ASC-3836
According to Chris Peyer who works at Adobe:
This is the expected behavior. Vector.
and Vector. are unrelated types no matter what the relation is between Example and ExampleA.
It seems to me that this argument can only stem from the idea that if one object is composed of Fruits and another composed of Apples, that does not mean that the two objects are themselves related:
public class MarketStall
{
public var forSale:Apple;
}
public class Supermarket
{
public var forSale:Fruit;
}
There is no relationship between MarketStall and Supermarket in this code.
Surely though the relationship between two Vectors defined by a generic is much stronger than that? The Vector is nothing more than an indexed plurality of the objects that compose it. We know it doesn’t have other unrelated functionality. We can do this:
var fruits:Vector.<Fruit> = new Vector.<Fruit>(); for each (var apple:Apple in apples) fruits.push(apple);
without problem, but not this:
var fruits:Vector.<Fruit> = apples as Vector.<Fruit>;
Most importantly however, can’t the Adobe engineers see how damned useful it would be if we could cast Vectors like this? Languages should not get in the way of what is intuitively right. You should not have to struggle with the language like this, or find workarounds. Flash developers are almost always looking for improvements in speed and memory, but here you the language makes you waste memory and take time. Or, use the other workaround and avoid Vector and use Array instead. That’s what I did; what a triumph, Adobe!
Please Vote On This Bug
I’ve been here before! My former Colleague Vizio360 rediscovered this bug about buttonMode=true preventing unloading of SWF files, but it too was resolved by being called Not A Bug. The clamour for it to be reopened has so-far fallen on deaf ears, but the only way anyone will ever take any notice is if you vote and comment.
Currently Adobe consider this issue closed. Pehaps if you comment on the post they will give it second thoughts. (this is edited text after it was pointed out to me that you can’t vote for closed bugs)



Pingback: Gotcha – transform.matrix and scaleX/Y at AlecMcE.com
Pingback: Why we need Generics in AS3 at AlecMcE.com