简体   繁体   中英

Get type of an object in a Haxe macro

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());

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM