views:

243

answers:

2

Custom metadata in Actionscript classes are very cool. You can put anything in square brackets in front of classes, functions, and variables; tell the compiler to include it (by name) in the SWF; and use describeType() to retrieve it.

[MyMetaData(name1=value1, name2=value2)]

Add to "Additional compiler arguments”

-keep-as3-metadata+=MyMetaData

And use describeType() to find those items with that meta-data

var typeDescriptionXML : XML = flash.utils.describeType(aType);

var itemsWithMyMetaData : XMLList = 
    classXML.factory.metadata.(@name == "MyMetaData");


Static functions, on the other hand are rather straight-forward:

public static function myStaticFunction() : Object
{
    var result : Object = new Object({name1: "value1", name2: "value2"});
    return result;
}

While I would like static functions to be virtual, they do what they are supposed to do.


What advantage does meta-data have over static functions for class level items?

+2  A: 

I would assume there are some performance issues related to using metadata. describeType isn't cheap, then you have to pull what you want out of the large XML tree.

There are certainly type related issues. Even your second example where you send an Object would be much better if the data was inside a class. It is very rare for me these days to use Objects anywhere in my code except when I am prototyping. And your code could be much more succinct:

public static function myStaticFunction():Object
{
    return { name1: "value1", name2: "value2" };
}

Additionally static class functions can interact with other static class functions and static properties:

package
{
public class StaticClass
{
    private static const SOME_DATA:int = 0;
    private static const SOME_STRING:String = "Hello World";

    private static var count:int = 0;

    public static function printCount():void
    {
        trace(SOME_STRING, SOME_DATA, count++);
    }
}

}

I can imagine uses for metadata (e.g. for library code that doesn't require implementing an interface) and I can see why Flex uses them for that purpose (e.g. build lists of remote-objects, events, bindables) but I would never recommend them for normal use.

James Fassett
+1  A: 

I moreless agree with James. However, there is one advantage for metadata: uniform access ...

if you have an array of classes, and you kind of know, that each of them has a property classMetaData, then this code won't work:

for each (var c:Class in arrayOfClass) 
     trace(c.getData());

this will:

for each (var c:* in arrayOfClass) 
     trace(c.getData());

but it's rather quirky, requiring that EVERY class has that static property, and on top of that c.getData() is also of type * ...

accessing metadata through describeType always works good ... the worst thing that can happen is, that you wind up iterating an empty XMLList, but there's nothing wrong with that in the end ... if you encapsulate that traversal into some utility class or something like that, then you end up with a solid solution ... and if it uses a cache for retrieved data, you are not really slower than with static variable access ...

in the end, i think the idea is simple: you should use metadata-tags to capture meta-data and actionscript to capture data ... the distinction is not 100% obvious, but in the end, it's up to you to draw the line between the two ...

back2dos