Macro FAQ examples

How can I configure my macros from command line?


Config.hx


class Config {
    static var values = new Hash();

    @:macro static public function set(key:String, value:String) {
        values.set(key, value);
        return null;
    }

    static public function get(key:String)
        return values.get(key)
}

Command line


haxe --macro Config.set('key','value')

Usage (within a macro)


trace(Config.get('key')); // "value"

Things to learn from this

  • command line macros are guaranteed to execute before any normal macro does.
  • all macros share the same context, which (among other things) means that static values are retained between them.
  • macros accept constant arguments, such as strings, without any Expr detour.
  • you can remove @:macro and thus also return null; when using command line macros. It is left here to clarify that we're entering macro context.
version #14187, modified 2012-05-31 00:36:02 by Simn