fork download
  1. import macros, std/math
  2.  
  3. macro apply*(f: typed, xs: untyped): untyped =
  4. echo f.treeRepr
  5. let funcParams = params(getImpl(f))
  6.  
  7. var i: int = 0
  8. var args: seq[NimNode] = @[]
  9.  
  10. result = newCall(f)
  11.  
  12. for child in funcParams.children:
  13. if child.kind == nnkIdentDefs:
  14. result.add(quote do: `xs`[`i`])
  15. i += 1
  16.  
  17. proc f(a: int, b: int, c: int) : int =
  18. a + b + c
  19.  
  20. proc mypow(a: float, b: float): float =
  21. pow(a, b)
  22.  
  23. when isMainModule:
  24. echo $apply(f, @[20.int, 5.int, 10.int])
  25. echo $apply(pow, @[2.float, 32.float])
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Sym "f"
ClosedSymChoice
  Sym "pow"
  Sym "pow"
stack trace: (most recent call last)
prog.nim(5)              apply
prog.nim(25, 14) template/generic instantiation from here
prog.nim(5, 34) Error: node is not a symbol
stdout
Standard output is empty