2

I've been learning a fair bit of C# lately and noticed that the <> syntax is used a lot, eg:

Content.Load<AssetType>("asset name");

The only place I've seen this used in AS3 is when using Vectors:

var enemies:Vector.<Enemy> = new Vector.<Enemy>();

Can I implement use of this syntax myself somehow in ActionScript 3? For example, I may want my own method similar to Content.Load().

Marty
  • 39,033
  • 19
  • 93
  • 162
  • I dont see how other than doing some crazy script before you compile, in a similar way to how haxe has tools to convert languages . The syntax of any programming language is by design it sounds like you want to create your own language ;) – imp Dec 28 '11 at 15:43
  • @imp You'll notice that I'm asking specifically if there is any *inbuilt* use for `<>` other than when working with Vectors that may be similar to that of C#. – Marty Dec 29 '11 at 00:37
  • sure I have never seen the syntax in anything but Vector. I have not written any C# before, I am curious what do you see the value of this ? – imp Dec 29 '11 at 01:41
  • Would be useful for making my own type of Vector, maybe via something like `List` so I can add some methods like `remove(item:T):void`. Currently I can't see a way to create Vectors dynamically. – Marty Dec 29 '11 at 01:53
  • ah k cool, haxe has generics built in http://haxe.org/ref/type_params , its worth a look. I see it as the future for as3 developers wanting a cross platform development language. With haxe you can target any platform you are currently doing with as3 and well much more. Or you could vote for the issue for Adobe to address well never ;) http://bugs.adobe.com/jira/browse/ASL-115 – imp Dec 29 '11 at 03:23

3 Answers3

1

The only other place where the angled braces are used (as far as I know) is for declaring inline XML:

var myXML:XML = <rootNode><dataNode>What's up?</dataNode></rootNode>;

Which is horrible programming practice. I don't believe there is any way to extend AS3 in the manner you describe, as it is only possible to create class extensions, not entirely new language syntax.

Josh at The Nerdery
  • 1,397
  • 9
  • 16
1

The syntax you're referring to is called generics, and Vector is the only way they can currently be used in AS3.

Here is a link to a related question about AS3's generics, why you can't create your own.

Hope this helps!

Community
  • 1
  • 1
31eee384
  • 2,748
  • 2
  • 18
  • 27
1

As far as I know, the Realaxy editor provides generics through a language extension. The dev talks about it here (see the comments)

The only problem is that it ties you to the editor.

That said, you could also probably fake it through reflection, or just doing your own runtime checking. Not a perfect solution though

divillysausages
  • 7,883
  • 3
  • 25
  • 39