haxe-continuation multiple return values

325 views
Skip to first unread message

Vadim Breslavets

unread,
Dec 11, 2014, 5:07:20 AM12/11/14
to haxe...@googlegroups.com
Hello.
I have a question about haxe-continuation

The problem is that async functions often (in NodeJs almost always) return several values in callback (e.g. first value in callback is error if any, and second computed value):
function (param1, cbFunc) {
if( true ) {
return cbFunc(null, 'OK');
}
else {
return cbFunc ('error', null);
}
}

But if we use @:async function(){} metadata, we can return only one value in return statement.

Haxe source function:
@:async function (param1):Dynamic{
if( true ) {
return OBJ;
}
else {
return OBJ;
}
}

Function transformed by continuation macro becomes:
function (param1, cbFunc:Dynamic->Void):Void{
if( true ) {
cbFunc(OBJ);
return;
}
else {
cbFunc(OBJ);
return;
}
}

So we can call callback with only one argument.

Is there a way to use @:async metadata and return (call callback) with multiple arguments?
Thanks.

Juraj Kirchheim

unread,
Dec 11, 2014, 9:46:13 AM12/11/14
to haxe...@googlegroups.com
How about this?

@:async function (param1):{ a:A, b: B }
if( true ) {
return { a: someA, b: someB };
}
else {
return { a: someOtherA, b: someOtherB };
}

Best,
Juraj


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Vadim Breslavets

unread,
Dec 11, 2014, 10:00:38 AM12/11/14
to haxe...@googlegroups.com
That's, exactly what I did but it looks not so pretty. The problem is that macro forces function conversion. It would be good if it could leave function signature intact.
Because library itself allows multiple return values:

var err:Dynamic, data:Dynamic = @await getMultipleAsync( params );

function getMultipleAsync (param1, cbFunc) {
if( true ) {
return cbFunc(null, 'OK');
}
else {
return cbFunc ('error', null);
}
}

But it works only for functions with callback but without @:async metadata (which means that function can't call other CPS funcs i.e. can't contain @await itself).

杨博

unread,
Mar 18, 2015, 10:52:47 AM3/18/15
to haxe...@googlegroups.com
I added a helper class Tuple for your situation.


// Don't declear the return type if you want multiple return values
@:async function returnTwoParameters(param1) {
  return @await Tuple.pair(someA, someB);
}


在 2014年12月11日星期四 UTC+8下午11:00:38,Vadim Breslavets写道:

杨博

unread,
Mar 18, 2015, 10:53:42 AM3/18/15
to haxe...@googlegroups.com
An example: https://github.com/Atry/aws-ec2-instance-pool/blob/master/awsEc2InstancePool/Ec2InstancePool.hx#L74

在 2015年3月18日星期三 UTC+8下午10:52:47,杨博写道:

杨博

unread,
Mar 18, 2015, 11:04:24 AM3/18/15
to haxe...@googlegroups.com
I actually implemented the tuple feature in 2012, the first version of haxe-continuation. See https://github.com/Atry/haxe-continuation/blob/haxe-3.1/tests/TestContinuation.hx#L253

Unfortunately I have never documented the feature.

在 2015年3月18日星期三 UTC+8下午10:53:42,杨博写道:

杨博

unread,
Mar 18, 2015, 11:20:49 AM3/18/15
to haxe...@googlegroups.com
Let me tell you more undocumented features:

function fourParameter(a, b, c, d):Void {}
@:async function foo() {
// Result: [ "head", "someA", "someB", "tail" ]
var a = [ "head", @await Tuple.pair("someA", "someB"), "tail" ];

// It compiles!
fourParameter("head", @await Tuple.pair("someA", "someB"), "tail");} 
}
Reply all
Reply to author
Forward
0 new messages