views:

99

answers:

3

What language construction can be used to make a compile time checking of Array elements type when the Array is a function parameter?

Let's use this function as an example:

[ArrayElementType("String")]
private function GetNumberArray(parameter:Array):Array {
   var myData:Array = [1.0, 2.0, 3.0];
   return myData;
}

Here we've marked the returned Array as one containing elements of type string. Is there a way to mark the parameter variable as an Array containing elements of certain type? I'm specially interested in the Array collection. I'm aware of the vector collection but I have reasons not to use it in my case.

A: 

Flash 10 supports vectors.

Use Vector<String> instead ;)

Adrian Pirvulescu
A: 

You could use a Vector, although that has a slightly different type than an array:

private function GetNumberArray(parameter:Vector.<String>):Vector.<int> {
    var myData:Vector.<int> = Vector.<int>([1.0, 2.0, 3.0]);
    return myData;
}
James Fassett
A: 

There seems to be no way to apply the attribute on the parameter. If you try to apply it as shown bellow you'll get compiler error.

[ArrayElementType("String")]
private function GetNumberArray(
   [ArrayElementType("String")] 
   parameter:Array):Array
{
   var myData:Array = ["1.0", "2.0", "3.0"];
   return myData;
}
Branislav Abadjimarinov