The Type-Safety of Vectors
I really want to like Vectors, the new typed array functionality in Flash 10. In fact, I use it as often as I can. But there are some really awkward things about it that make it a pain. Here are some gotchas:
The point of Vectors is supposed to be twofold: type safety and speed. In empirical tests, they do appear somewhat faster than Arrays, Dictionaries, and Objects. They don’t always hold up so well in the type safety category though. Consider this obvious typo:
var vec:Vector.<int> = new Vector.<String>();
That seems like about the most obvious compiler error you could imagine, right? Wrong. You don’t get a compiler error or warning, but instead a runtime exception:
TypeError: Error #1034
How about if you try to put an incorrect type in the Vector?
var vec:Vector.<DisplayObject> = new Vector.<DisplayObject>(); vec.push(33); vec.unshift(33);
Both of these result in the same TypeError as above! How about this?
var vec:Vector.<DisplayObject> = new Vector.<DisplayObject>(); vec[0] = 33; var val:int = vec[0];
Sanity prevails here and you actually get compiler errors:
Error: Implicit coercion of a value of type int to an unrelated type flash.display:DisplayObject.
and
Error: Implicit coercion of a value of type flash.display:DisplayObject to an unrelated type int.
So what makes push() and unshift() so different that they give compiler errors and warnings? The answer is that they will both take as many elements as you want to push/unshift onto the Vector. Since this is untyped, your error isn’t caught until runtime.
Ultimately, Vectors are type-safe. Stronger type safety like you get in C++ and Java still eludes AS3. The trick, until the situation changes, is to know the above subtleties of Vector so you’re not caught with a crash.
#1 by skyboy on October 21st, 2010 ·
What should this do? Throw an error? Attempt to add the Sprites to the vector? Attempt to add the other vector to the vector?
It kind-of does the last one. It will trace
1 0
.According to the documentation you pass in var args or a vector of objects to be spliced into it.
This is wrong. It takes var args, and nothing more, but it also does something weird. It converts everything to the type specified, even if it’s unrelated.
#2 by skyboy on October 21st, 2010 ·
Bah. HTML safety got me, the first vector is int, the second is Sprite; an easier way to prevent HTML-injection is to replace less-than with
& lt ;
and greater-than with& gt ;
without spaces when it isn’t an allowed tag; after & is replaced with& amp ;
of course.#3 by jackson on October 21st, 2010 ·
I fixed the formatting on your post. I should really see if there’s a way I can improve the standard WordPress comment filter to allow better code posting…
Anyhow,
splice
is working just likepush
would work. It doesn’t throw anError
, but instead does as you say and converts the argument(s) to theVector
’s type. Consider this:That also outputs
1 0
because the pushed type (Vector.<Sprite>
) converts to the integer0
. This is totally contrary to Adobe’s documentation, which states that an exception will be thrown.You are also correct in that
splice
’s third argument does not do as the documentation says as it doesn’t add the elements of the passedVector
, but rather converts like you showed. It’s not even a case of a type mismatch, as in your example, as this results in the same output as your example:In summary,
Vector
is still, as I stated in the article’s conclusion, type-safe. It just doesn’t behave as the documentation says it does and is therefore pretty strange to work with. Thanks for the insightful comments!