macro: how to determine if an "if" or a "switch" Expr is Void type?

19 views
Skip to first unread message

Munir Hussin

unread,
Jun 13, 2016, 12:22:30 PM6/13/16
to Haxe
Hello. I need help in determining if something is a statement or an expression. I have a macro like so

public static macro function SomeMacro(expr:Expr):Expr
{
    return expr.map(someTransformation);
}

A switch and an if can be either an expression that returns something, or it could be Void. I would like to transform them differently whether its Void or not. In the example below, if foo or bar is Void, then the if is a Void and cannot be used as an expression.

SomeMacro(function(a:Int, b:String) {
    ...
    if (a) foo(a) else bar(b);
    ...
});

So in my someTransform function, I used Context.typeof(expr) and compared it with Void. But Context.typeof(expr) itself is giving an error, "Unknown identifier : a" (using the example above). I suppose that's because a is within the macro. If I replace the a and b with constants, and foo and bar with trace or other functions defined outside the SomeMacro call, then I can reliably use Context.typeof(expr).

This is the function I call to do the type check, which doesn't work in the scenario I mentioned above:

public static function isVoidExpr(e:Expr):Bool
{
    var exprType = Context.typeof(e).toComplexType();
    return exprType.match(TPath({ name: "StdTypes", pack: [], params: [], sub: "Void" }));
}

Is there a better way to do this Void check?

I'm attempting to do a Generator and Fiber macro, so I'm trying to write macros to transform regular functions into a resumable function. It sort of works when I assume all if/switch are either statements or expressions, but if they're used both ways, then there's compilation errors.
Reply all
Reply to author
Forward
0 new messages