Metadata
It is possible to specify some metadata before the following :
- classes and enums declarations
- classes fields and methods (both static and member)
- enums constructors declarations
These metadata will be compiled and stored in generated source/binary files, and can be later retrieved at runtime to implement any specific behavior that your application might require.
Metadata are prefixed with the @ sign, followed by a valid indentifier, and might have a list of optional parameters. Here's an example :
@author("Nicolas") @debug class MyClass { }
The metadata parameters can be any constant value among the following :
- Int, Float, Bool, String, null values
- Arrays
- Anonymous objects (structures)
You can also reference inline variables/functions as long as they produce a constant value.
API
You can access the metadata at runtime by using the haxe.rtti.Meta API.
The getType function will return the metadata associated with a given class/enum declaration. For instance, given previous example :
var m = haxe.rtti.Meta.getType(MyClass); trace(m); // { author : ["Nicolas"], debug : null }
The getFields will return metadata associated with either the given class member fields or the given enum constructors. Example :
class Foo { @values(-1,100) var x : Int; static function main() { var m = haxe.rtti.Meta.getFields(Foo); trace(m.x.values); // [-1,100] } }
The getStatics will return metadata associated with the given class static fields or methods.
You can find the way Metadata can also be used to tweak compiler in Tips and Tricks section.