Metadata
It is possible to specify some metadata before the following :
- class and enum declarations
- class fields and methods (both static and member)
- enum constructor declarations
This metadata will be compiled and stored in the generated source/binary files, and can be retrieved later at runtime to implement some specific behavior that your application might require.
Metadata is prefixed with the @ sign, followed by a valid identifier, and might have a list of optional parameters. Here is an example :
@author("Nicolas") @debug class MyClass { }
Each metadata parameter can be any constant value of one of the following types:
- 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 the 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 static fields or methods of the given class.
Metadata can also be used to tweak the compiler. Metadata which is prefixed with "@:" is only available at compile time, and is used by the compiler or can be used by macros, but it will not be included in the generated code. A list of the built-in compiler tweaks that can be configured with metadata can be found in the Tips and Tricks section.