Last active
August 29, 2015 14:22
-
-
Save mcheshkov/581b4645cd5fc37b6088 to your computer and use it in GitHub Desktop.
Short lambda macro #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Line 3 is using string interpolation, i
is an argument of the other
function.
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
First two Lambda.iter calls is same thing.
Left i (from arrow) - argument name.
Right i - using that name as identifier to call trace.