haxetink / tink_await

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

Nested switch problem? #3

Closed kevinresol closed 8 years ago

kevinresol commented 8 years ago

Not sure about the cause, but I isolated this piece of failing code as follow:

package;

using tink.CoreApi;

class Main implements await.Await{
    public static function main() {
        new Main();
    }

    public function new() {
        bar().handle(function(o) trace(o.sure()));
    }

    @:async function bar() {
        var s = switch outcome() {
            case Success(s): s;
            default:
                switch array() {
                    case null:
                    case v:
                        var s = null;
                        for(a in v) {
                            s = a;
                        }
                        s;
                }
        }

        var f = @:await foo();

        return s == null ? 0 : Std.parseInt(f);
    }
    function array() return [for(i in 0...5) Std.string(i)];
    function string() return "2";
    function bool() return true;
    function outcome() return Success('3');
    @:async function foo() return '1';

}
benmerckx commented 8 years ago

It's the case null: in switch array() which doesn't result in a value for var s. This should probably result in a compile error. I will look into it.

kevinresol commented 8 years ago

Oops, that piece of code was problematic. Try this instead

package;

using tink.CoreApi;

class Main implements await.Await{
    public static function main() {
        new Main();
    }

    public function new() {
        bar().handle(function(o) trace(o.sure()));
    }

    @:async function bar() {
        var s = switch outcome() {
            case Success(s): s;
            default:
                var ret = null;
                switch array() {
                    case null: // do nothing, leave `ret` as null
                    case v: for(a in v) ret = a;
                }
                ret;
        }

        var f = @:await foo();

        return s == null ? 0 : Std.parseInt(f);
    }
    function array() return [for(i in 0...5) Std.string(i)];
    function string() return "2";
    function bool() return true;
    function outcome() return Success('3');
    @:async function foo() return '1';

}
benmerckx commented 8 years ago

I see, I'll get this fixed!

benmerckx commented 8 years ago

There were a few things going wrong here, which are addressed in 0.0.5. Switch cases that do not return a result when it's needed should also error now.

kevinresol commented 8 years ago

Awesome. seems working now. Thanks!