When a Vector is casted like this…
var v1:Vector.<String> = new Vector.<String>();
v1.push("foo");
var v2:Vector.<Object> = Vector.<Object>(v1)
v1.push("bar");
trace(v1); //foo,bar
trace(v2); //foo
… a copy of the Vector is created, as you can see in the trace output.
But when you change line 3 to…
var v2:Vector.<*> = Vector.<*>(v1)
… no copy is created, both v1 and v2 point to the same object, trace outputs will both be “foo,bar”.
Why that? Shouldn’t there somehow be a consistent behaviour?
What you have to realize is that
Vector.<Something>(Vector.<Anotherthing>)is not a type cast. In fact, you can’t cast a vector of one kind to a vector of another kind at all! Try this:You also can’t assign a vector of subtypes:
That is because while String is a subtype of Object, the two kinds of vectors are not related.
So why doesn’t your notation fail? Because what you are really doing is calling the top level function
Vector(), which creates a vector from any collection type data you pass in. The documentation for it says:The asterisk in your type declaration is a place holder, so the variable will hold any typed instance of Vector, and that allows for the function to return the same vector you passed in.