Haxe Code Cookbook
Haxe programming cookbookMacrosAdd a static field

Add a static field

Reading time: 0.5 minute

Virtually adds this static variable to a class:

public inline static var STATIC_VAR:Float = 1.5;

Build macro

import haxe.macro.Context;
import haxe.macro.Expr;

class MyMacro {
  public static function build():Array<Field> {
    // get existing fields from the context from where build() is called
    var fields = Context.getBuildFields();
    // append a field
      name:  "STATIC_VAR",
      access:  [Access.APublic, Access.AStatic, Access.AInline],
      kind: FieldType.FVar(macro:Float, macro $v{1.5}), 
      pos: Context.currentPos(),
    return fields;


class Main {
  public function new() {
    trace(Main.STATIC_VAR); // 1.5;

Last modified:
Category:  Macros