haxetink / tink_await

Haxe async/await
MIT License
58 stars 15 forks source link

Possible to use @:await inside expression macros? #13

Open kevinresol opened 8 years ago

kevinresol commented 8 years ago
// Main.hx
@:await
class Main {
    @:await static function main() {
      var s:String = Macro.expr();
    }

    @:async static function string() return 'hi';
}

// Macro.hx
class Macro {
    public static macro function expr() {
        return macro @:await string();
    }
}

The above code won't work because the await macro runs first. After that the typer kicks in and transform the macro call to actual expression. As a result, the generated expression (@:await string()) won't get handled by the await macro.

So, are there any workarounds for that?

kevinresol commented 8 years ago

Is it possible to have a "expression-level" version of tink_await. That I can apply the transformation with a macro call? like so:

var surprise = Await.manuallyTransform({
  // my block of code:
  @:await string();
});

public macro static function manuallyTransform(expr:Expr) {...}
kevinresol commented 8 years ago

The above example won't work because even so the outer macro will run first (if I supply a macro call as its parameter)

However, if somehow we can manually execute an expression macro, it may work. https://github.com/HaxeFoundation/haxe/issues/5534

benmerckx commented 8 years ago

Well, I had a look at that when https://github.com/haxetink/tink_await/issues/10 popped up. Seems like expression macros run when you use Context.typeExpr on an expression. But it only worked when applied to every expression, not the full body. It also failed multiple times with errors I couldn't catch. Anyway, that's when I gave up :) So we'll have to wait until haxe gives us a proper way to run all expression macros during type building.