3 ms·
Note: Haxe does not typecheck macros. See https://haxe.org/manual/macro-ExprOf.html https://haxe.org/manual/macro-ExprOf.html For example, this seemingly ill-
by klntsky 8y ago
Note: Haxe does not typecheck macros.
See https://haxe.org/manual/macro-ExprOf.html 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};
}
}
- k_ 8y agoThat's not entirely true. `ExprOf<T>` itself is not checked in either the macro function argument or return type, but that's only meant for static extension (and maybe some other features?). 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 http://try-haxe.mrcdk.com/#48EDb).
- nadako 8y ago> Haxe does not typecheck macros. 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 https://haxe.org/manual/expression-type-check.html
- b2d 8y agoIncorrect. To quote the page you've linked: "For the most part, this type is identical to Expr, but it allows constraining the type of accepted expressions." or otherwise put: ExprOf only affects macro arguments, not return value. What you want is this: class StringGetter { public static macro function getString() { var s:String = ""; return macro ($v{s} : Int); } }