Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get type of an object in a Haxe macro

Tags:

macros

haxe

I would like to get the class of an object in a macro so that I can access its static variables:

// autoBuild macro adds static field "id_ : Int" to all subclasses
class Base {

}

class Child1 extends Base {
  public function new() {}
}

class Child2 extends Base {
  public function new() {}
}

class Container {
  public function addChild(index: Int, object: Base) {}

  macro function add(object: ???) {
    // find out class of on object
    // ???
    // var id = class.id_;
    this.addChild(id, object);
  }
}

Desired usage:

var c = new Container();
c.add(new Child1());
c.add(new Child2());
like image 576
montonero Avatar asked Oct 18 '22 09:10

montonero


1 Answers

You can use Context.typeof() to get the expression's type - then you need to do a bit of pattern matching to find out the type's name. The following only works with classes because it only matches TInst, but could be extended:

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

class Container {
    // [...]

    public macro function add(self:Expr, object:Expr):Expr {
        var name = switch (Context.typeof(object)) {
            case TInst(_.get() => t, _): t.name;
            case _: throw "object type not found";
        }
        return macro $self.addChild($i{name}.id_, $object);
    }
}

This will generate the following code:

var c = new Container();
c.addChild(Child1.id_, new Child1());
c.addChild(Child2.id_, new Child2());

Note that accessing _id via it's unqualified name is only safe if it's actually imported (or toplevel) - in practice you'd want to use t.pack in combination with $p{} to generate the fully qualified path.

like image 53
Gama11 Avatar answered Oct 23 '22 20:10

Gama11