by klntsky · 7y ago · 0 comments
See https://haxe.org/manual/macro-ExprOf.html
For example, this seemingly ill-typed definition (which should return Int as suggested by the type paramater, but in fact returns a string) successfully typechecks:
class StringGetter {
public static macro function getString():haxe.macro.Expr.ExprOf<Int> {
var s:String = "";
return macro $v{s};
}
} class StringGetter {
public static macro function getString() {
var s:String = "";
return macro ($v{s} : Int);
}
}Expressions generated by your macros will be parsed by the type checked. You won't be able to do
var s:Int = StringGetter.getString();
Because you'll get a compilation error there (http://try-haxe.mrcdk.com/#48EDb).That is a weird statement. Haxe expression macros by definition return untyped AST to be processed further normally by the typer, just like hand-written code. And it will be type-checked. And then there's an AST node existing exactly for type-checking (or, rather, type unification) that you can generate to insert custom checks: https://haxe.org/manual/expression-type-check.html