Can someone explain why this won't work? Thanks.
macro testLam(f:expr, args:varargs[expr]):expr =
var call = newNimNode(nnkCall)
call.add(newIdentNode(f.ident))
for i in 0..args.len-1:
if args[i].kind == nnkIntLit:
call.add(args[i])
call.add(newIdentNode(!"x"))
result = newNimNode(nnkLambda).add(
newEmptyNode(),
newEmptyNode(),
newEmptyNode(),
newNimNode(nnkFormalParams).add(
newIdentNode(!"int"),
newIdentDefs(newIdentNode(!"x"),newIdentNode(!"int"))), ##params
newEmptyNode(), ## pragmas
newEmptyNode(),
newStmtList(call))
var baz = testLam(sum,10)
echo baz(4)
OK, I got the following to compile. I don't understand why I have to re-package the intLitNode again. If, the expression f is a proc that I call with the passed parameters in args, and the proc expects an argument of int, why can't I just pass it args[i]?
macro testLam(f:expr, args:varargs[expr]):expr =
var call = newNimNode(nnkCall)
call.add(newIdentNode(f.ident))
for i in 0..args.len-1:
if args[i].kind == nnkIntLit:
call.add(newIntLitNode(args[i].intVal))
call.add(newIdentNode(!"x"))
result = newNimNode(nnkLambda).add(
newEmptyNode(),
newEmptyNode(),
newEmptyNode(),
newNimNode(nnkFormalParams).add(
newIdentNode(!"int"),
newIdentDefs(newIdentNode(!"x"),newIdentNode(!"int"))), ##params
newEmptyNode(), ## pragmas
newEmptyNode(),
newStmtList(call))