views:

47

answers:

3

I'm looking for method to create Vector and push some values without defining variable Vector. For example:

I have function:

public function bla(data:Vector.<Object>):void { ... }

this function expects Vector as parameter. I can pass parameters this way

var newVector:Vector.<Object> = new Vector.<Object>();
newVector.push("bla1");
newVector.push("bla2");
bla(newVector);

Can I do it in one line in Flex? I'm looking for something like:

bla(new Vector.<Object>().push("bla1").push("bla2"));

I've also tried this:

bla(function():Vector.<Object> { var result:Vector.<Object> = new Vector.<Object>(2, true); result.push("bla1"); result.push("bla2"); return result; });

But it complains:

1067: Implicit coercion of a value of type Function to an unrelated type __AS3__.vec:Vector.<Object>...

Thanks

A: 

I'm not really sure why you'd need to do it one line. You could always write a wrapper class if you happen to do this often. The wrapper class could have a push method that returns a reference to the original object so you can use the first method you wanted.

You could also write a helper function which created a new vector and added the elements to the vector and then returned the vector.

Is there a particular need for wanting this on one line?

Pace
A: 

You are not able to do this:

bla(new Vector.<Object>().push("bla1").push("bla2"));

because the "push" method returns the length of the Vector. So what this means is that you are trying to push the String "bla2" onto the int 1. This won't work!

And your next example is passing a function to the bla method, not calling that function and passing the returned Vector.

Also you are saying the Vector type is "Object" but you are passing in Strings. You should do this:

Vector.<String>

You could do something like this:

function getVector():Vector.<String>
{
    var newVector:Vector.<String> = new Vector.<String>();
    newVector.push("bla1");
    newVector.push("bla2");
    return newVector;
}

bla( getVector() );
TandemAdam
+1  A: 

You can't chain Vector.push() calls as they return uint's -- the new vector length.
The coercion problem, on the other hand, happens because you are passing a function to the bla function, which expects a Vector.<Object>.

You could fix that easily:

bla((function():Vector.<Object> {
    var result:Vector.<Object> = new Vector.<Object>(2, true);
    result.push("bla1");
    result.push("bla2");
    return result; })()
);

However, there's already a top level function in AS3 that helps you creating vectors. The Vector() function expects either an Array or a Vector and returns a Vector. So, for example, you could use:

bla(Vector.<Object>(['bla1', 'bla2']));

Visit the AS3 Reference for more info.

EDIT: I forgot to mention that the fix on the function approach was simply adding a () to it, meaning we actually called the anonymous function and passed it's return to the bla function.

MrKishi
Thanks, I found it myself already. Vector() function did that trick for me.
zdmytriv