haxetink / tink_await

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

Type info lost from a switch #12

Closed kevinresol closed 8 years ago

kevinresol commented 8 years ago
@:await
class Main {

  @:await static function main() {
    var value = switch @:await string() {
      case 's':
        var i = @:await int();
        $type(i); // gives `Int` correctly
        i;
      default:
        throw 'error';
    }
    $type(value); // becomes `Unknown` :(
  }

  @:async static function string() return 's';
  @:async static function int() return 1;
}

$type(i) gives Int correctly, but when it is assigned to value, $type(value) becomes Unknown.

benmerckx commented 8 years ago

The main function gets compiled to the code snippet below. I think the type might get lost because of the continuation of the default case. Is this causing you serious problems? Because I'm unsure if I can get this fixed. Typing it as var value: Int = switch ... will help.

string().handle(function(__t2) {
        var __t2_result;
        switch tink.await.OutcomeTools.getOutcome(__t2) {
                case Success(v):{
                        __t2_result = v;
                };
                case Failure(e):{
                        throw e;
                        return;
                };
        };
        {
                function __t0(__t1) {
                        var value = __t1;
                        $type(value);
                };
                switch __t2_result {
                        case 's':int().handle(function(__t3) {
                                var __t3_result;
                                switch tink.await.OutcomeTools.getOutcome(__t3) {
                                        case Success(v):{
                                                __t3_result = v;
                                        };
                                        case Failure(e):{
                                                throw e;
                                                return;
                                        };
                                };
                                {
                                        var i = __t3_result;
                                        $type(i);
                                        __t0(i);
                                };
                        });
                        default:__t0({
                                throw 'error';
                        });
                };
        };
})
kevinresol commented 8 years ago

This is not a big problem for me, I can just type hint it explicitly.

kevinresol commented 8 years ago

It is actually not caused by the default case, but by the fact that the function __t0 is not type-hinted. It can be demonstrated in the following example.

class Main {
  static function main() {
    function t(v) {
      $type(v); // Unknown
    }
    t('str');
    t(throw 0);
  }
}