How to convert haxe.macro.Type to Expr?

var clazz = Context.getType("SampleClass");
var expr = macro $clazz;

haxe.macro.Type should be haxe.macro.Expr

var clazz = Context.toComplexType(Context.getType("SampleClass"));
var expr = macro $clazz;

Null<haxe.macro.ComplexType> should be haxe.macro.Expr

I am trying to convert Type to Class so I can write a class to a variable.

ComplexType is part of the haxe.macro.Expr hierarchy. You can represent some, but not all haxe.macro.Type instances via haxe.macro.TypeTools.toComplexType.

and then slot your complex type where it belongs:

which will be as an annotation or a cast.

If you want a class directly, you’ll need to make a haxe.macro.TypePath and plug it into ENew

var clazz = Context.toComplexType(Context.getType("SampleClass"));
var expr = macro $clazz;

The code above creates a proper ComplexType object, so there is no exception or null return, but I get the error “Null<haxe.macro.ComplexType> should be haxe.macro.Expr”.

If you want to reference a class object such as some.pack.TheClass, you’ll need to construct the corresponding dot path syntax, which is an EIdent nested in a bunch of EFields.

The easiest way to do this is $p, i.e. macro $p{parts} where parts is an Array<String> of the individual path fragments. Example: Try Haxe!

I am trying to add classes to a map.