haxetink / tink_await

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

@async function must return something? #25

Closed andyli closed 5 years ago

andyli commented 5 years ago

Tested with Haxe 3.4.7 and tink_await 0.4.0.

Compile:

@await class Test {
    @async static function main() {
        return;
    }
}

Results in:

?:1: characters 1-7 : The macro didn't return a valid result

The error can be avoided by replacing return; with return null;. I guess it makes sense for tink_await to do the return; -> return null; transformation automatically.

kevinresol commented 5 years ago

Try

@await class Main {
    @await static function main() {
        if(@await foo() == 1) return;
        trace(2);
    }

    @async static function foo() return 1;

    static function check() {
        $type(main); // Void -> Void
        $type(foo); // Void -> tink.core.Promise<Int>
    }
}

There is a difference for @async and @await when used to annotate a function.

@async expects a return value and the function will be transformed into returning a Promise<T> @await doesn't expect a return value and its return type will be Void

kevinresol commented 5 years ago

And currently there is no check to enforce @async actually returns something for all the possible branches. But that is https://github.com/haxetink/tink_await/issues/6.

andyli commented 5 years ago

Ah, I see. Is the @async @await difference documented somewhere?

kevinresol commented 5 years ago

oops, looks like no. But there is an open issue for that https://github.com/haxetink/tink_await/issues/16

andyli commented 5 years ago

I guess copying your comment above into the README would be good enough.

kevinresol commented 5 years ago

Done, a bit rough but should be fine for now.

sonygod commented 4 years ago

Done !

use return null instead of return.