Created
April 16, 2013 19:50
-
-
Save deltaluca/5399030 to your computer and use it in GitHub Desktop.
convert long param functions into params object functions.
This file contains hidden or 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
package; | |
#if macro | |
import haxe.macro.Expr; | |
import haxe.macro.Context; | |
#end | |
@:autoBuild(LongParamsImpl.run()) interface LongParams {} | |
class LongParamsImpl { | |
#if macro | |
public static function run() { | |
var fields = Context.getBuildFields(); | |
for (f in fields) { | |
var longParams = false; | |
for (m in f.meta) { | |
if (m.name == ":longParams") { | |
longParams = true; | |
break; | |
} | |
} | |
if (!longParams) continue; | |
switch (f.kind) { | |
case FFun(fun): | |
var params = []; | |
var ftypes = []; | |
for (a in fun.args) { | |
var o = macro $p{["params",a.name]}; | |
var atype = if (a.type == null) Context.toComplexType(Context.typeof(a.value)) else a.type; | |
var opt = a.opt || a.value != null; | |
var value = if (opt) (macro if ($o==null) $e{a.value} else $o) else o; | |
params.push({ | |
expr: EVars([{ | |
name: a.name, | |
type: atype, | |
expr: value | |
}]), | |
pos: Context.currentPos() | |
}); | |
ftypes.push({ | |
pos: Context.currentPos(), | |
name: a.name, | |
meta: if (opt) [{name:":optional",pos:Context.currentPos(),params:[]}] else [], | |
doc: null, | |
access: [], | |
kind: FVar(if (opt) TPath({name:"Null",sub:null,pack:[],params:[TPType(atype)]}) else atype, null) | |
}); | |
} | |
params.push(fun.expr); | |
fun.expr = macro $b{params}; | |
fun.args = [{ | |
name: "params", | |
value: null, | |
type: TAnonymous(ftypes), | |
opt: false | |
}]; | |
default: | |
} | |
} | |
return fields; | |
} | |
#end | |
} |
This file contains hidden or 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
import Macros; | |
class Main implements LongParams { | |
@:longParams static function test(a:Int, b=20,c=30,d=40,e=50,f=60,g=70) { | |
trace([a,b,c,d,e,f,g]); | |
} | |
static function main() { | |
test({a:10}); | |
} | |
} | |
// Generated code from macro looks like: | |
// class Main implements LongParams { | |
// static function test(params:{a:Int, ?b:Int, ?c:Int, ?d:Int, ?e:Int, ?f:Int, ?g:Int}) { | |
// var a:Int = params.a; | |
// var b:Int = if (params.b == null) 20 else params.b; | |
// var c:Int = if (params.c == null) 30 else params.c; | |
// var d:Int = if (params.d == null) 40 else params.d; | |
// var e:Int = if (params.e == null) 50 else params.e; | |
// var f:Int = if (params.f == null) 60 else params.f; | |
// var g:Int = if (params.g == null) 70 else params.g; | |
// { | |
// trace([a,b,c,d,e,f,g]); | |
// } | |
// } | |
// static function main() { | |
// test({a:10}); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment