back2dos / tinkerbell

MIT License
83 stars 8 forks source link

question about generate function with parameters #42

Closed sonygod closed 11 years ago

sonygod commented 11 years ago

import test.test.Testx;

using tink.macro.tools.MacroTools;

class Main { static function main() {

//src/Main.hx:14: characters 2-14 : Invalid number of type parameters for test.test.Testx
 times_tink();

}   

//the tink way
@:macro static function times_tink():Expr {

    var inst = ENew( { pack:["test","test"], name:"Testx", params:[TPType("hello".asComplexType())] }, []).at();

    return  inst;

}  

} /**

  • package test.test;

class Testx { public var a = 1; public function new(?target:String) { trace("hello--"+target); }

} **/ what's wrong with my caller?thanks.

back2dos commented 11 years ago

You are mistaking type parameters for constructor arguments. It should be either of these:

ENew( { pack:["test","test"], name:"Testx", params:[] }, ["hello".toExpr()]).at();

"test.test.Testx".instantiate(["hello".toExpr()]);

macro new test.test.Testx("hello");
sonygod commented 11 years ago

how about this

@:macro static function testFunction():Expr { var f = (function say(age:Int,name:String):String { return name+age; }) .toExpr(); return f.func(["age".toArg("Int".asComplexType()),"name".toArg("String".asComplexType())], "String".asComplexType()).toExpr(); }

and the generate code

function(age : int,name : String) : String { return null; }

what's wrong ?