3

Seems like in AS3, I can in theory, delare arrays that can only contain certain data types:

private var my_array:Array.<String>;

produces this error:
1199: type parameters with a non-parameterized type

however

private var my_vector:Vector.<String>;  

is just fine.

Is there a way of making this work with an Array?

Frank
  • 459
  • 3
  • 9

4 Answers4

4

Unfortunately, no.

The "Generic" syntax is a one-off that was only given to Vector. It makes me sad. I'd use generics all over the place if I could.

Here is more discussion on the issue.

Community
  • 1
  • 1
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
4
[ArrayElementType("String")] 
public var newStringProperty:Array;

[ArrayElementType("Number")] 
public var newNumberProperty:Array;

http://livedocs.adobe.com/flex/3/html/help.html?content=metadata_3.html
unfortunately, such arrays don't provide access to methods of their element type like vectors do:

var strings:Vector.<String> = new Vector.<String>();
strings.push('hello world');
trace(strings[0].charAt(2));
www0z0k
  • 4,444
  • 3
  • 27
  • 32
  • That metadata tag only applies to MXML array definitions. It's effectively a compile-time constraint, not a runtime enforcement. And won't do anything if used in an as class. – J. Holmes Nov 08 '11 at 00:55
  • it'll do nothing in a pure as3 project, it worked for me in an .as class extending `UIComponent` – www0z0k Nov 08 '11 at 07:16
2

Arrays do not take type declarations at all at this time.

angelfilm entertainment
  • 1,155
  • 2
  • 15
  • 31
0

You can just pass elements of array to constructor. If you need parameterized "array", use vector.

zaynyatyi
  • 1,116
  • 6
  • 18