nadako / haxe-coroutines

41 stars 5 forks source link

Is it ok to automatically change the signature of a coroutine method? #12

Closed RealyUniqueName closed 5 years ago

RealyUniqueName commented 5 years ago

Automatically adding a continuation argument to the user's code may lead to unexpected surprises.

//no arguments defined by user
suspend function greeting() {...}

var collection:Array<Void->Void> = [greeting]; //Error: actually `greeting` is Continuation->Void

var fn:Void->Void = Reflect.field(this, 'greeting');
fn(); //Error: `greeting` expects one argument.
RealyUniqueName commented 5 years ago

The same problem with the return type:

suspend function loadJson(url:String):MyData { 
  var json = await(load(url));
  return Json.parse(json);
}

//outside of a coroutine
var data:MyData = loadJson(url); //Error: `loadJson()` does not return `MyData`, but something like `Promise<MyData>`
RealyUniqueName commented 5 years ago

It would be better to specify the exact coroutine implementation as a return type:

suspend function loadJson(url:String):Promise<MyData> { 
  var json = await(load(url));
  return Json.parse(json);
}
nadako commented 5 years ago

My idea was to use a @:coreType abstract for the suspend functions, like Suspend<Void->Void> which is incompatible with normal Void->Void, and then this type would have a magic start method or something from calling outside of coroutines. I thought I documented that, but I guess I only played with it in a toy implementation.

RealyUniqueName commented 5 years ago

Yes, that's documented. I missed it. But what about reflection?

nadako commented 5 years ago

But what about reflection?

Honestly, I would leave the reflection questions to those who want to use reflection :) Let's implement it for the typed code and then see how reflection-friendly it can be.