Skip to content

Instantly share code, notes, and snippets.

@mcheshkov
Last active August 29, 2015 14:22
Show Gist options
  • Save mcheshkov/581b4645cd5fc37b6088 to your computer and use it in GitHub Desktop.
Save mcheshkov/581b4645cd5fc37b6088 to your computer and use it in GitHub Desktop.
Short lambda macro #1
class ShortLambdaMacroTest {
static function other(i:Int){
trace('ok, so $i');
}
static function using(){
Lambda.iter([1,2,3], function(i){trace(i);});
Lambda.iter([1,2,3], f(i => trace(i)));
Lambda.iter([1,2,3], f(i => {trace(i + 1); other(i);}));
}
static macro function f(e){
switch(e.expr){
case EBinop(OpArrow, el, er):
var argName = "";
switch(el.expr){
case EConst(CIdent(i)):
argName = i;
default: throw "Unexpected arg $el";
}
return macro function($argName){ return $er; };
default: throw 'Unexpected $e';
}
throw false;
}
}
@ashes999
Copy link

ashes999 commented Jun 1, 2015

+1

Can you please explain the trace on line 3? What is i?

@mcheshkov
Copy link
Author

First two Lambda.iter calls is same thing.
Left i (from arrow) - argument name.
Right i - using that name as identifier to call trace.

@markknol
Copy link

markknol commented Jun 1, 2015

Line 3 is using string interpolation, i is an argument of the other function.

@mcheshkov
Copy link
Author

Oh, sorry, didn't see "line 3" part :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment