jdonaldson / promhx

A promise and functional reactive programming library for Haxe
MIT License
145 stars 24 forks source link

Promise.hx:51: Cannot use Void as value #81

Open mastef opened 8 years ago

mastef commented 8 years ago

On CPP ran into a weird issue where the then callback suddenly requires a return value when used after the static Promise.when

        Promise.when(p1,p2)
            .then(
                function(x, y)
                {
                    trace(x);
                    trace(y);
                    success();
                }
            )
            .catchError(function(x) trace(x));

Works with...

        Promise.when(p1,p2)
            .then(
                function(x, y):Bool
                {
                    trace(x);
                    trace(y);
                    success();
                    return true;
                }
            )
            .catchError(function(x) trace(x));

Any idea what could cause this?

jdonaldson commented 8 years ago

The "then()" method expects a return value, which becomes the type parameter for the Promise instance. If there is no parameter, it uses "Void", which isn't allowed as an instance type on some targets

The behavior here for cpp (and java) is technically correct, but I agree it's confusing in this case. I'll look into providing a more straightforward mechanism for registering a simple callback.

mastef commented 8 years ago

Ah if that's intended than that's ok! Just a note in the readme would do - was confused at first as then() statements on single promises don't seem to expect that return value :-)

mastef commented 7 years ago

As a followup - so this actually worked fine with Mac, Windows, Android and Neko, however fails on iOS :

If I have a thenable function where I exit early on by adding a return; ( as then statements can't be cancelled ) in an if statement - I'll run into the same error. When I use return true;, then I'll get another error as I don't have a return statement in the end of the function.

The problem isn't really the behaviour, but that from the error itself I don't know which class is causing it, as all I see is this : /usr/local/lib/haxe/lib/promhx/1,1,0/src/main/promhx/Promise.hx:55: characters 58-71 : Cannot use Void as value

Is there any way to highlight the culprit class? I have a bigger project with lots of promises that fail now when trying to port to iOS and it's hard to pinpoint all of them :

Windows.getChildren().then(function(tWindows:Array<Windowt>){

    // error that void, if just return; is used : 
    if(_active_tab_loading != _this_tab_loading) return true;

    // ... do something here ... //

    // error when previous return exists and this one is missing : //
    return true;

}).catchError(function(e:String){
    trace("error with promise", e);
});

It would be great if the error here would highlight that it happened in Windows.getChildren() or at least in the class that called it, and not in promhx/Promise.hx:55 ( compile time error so not getting a stacktrace )