t16758.nim 930 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. discard """
  2. errormsg: "'blk.p(a)' has nil child at index 1"
  3. action: reject
  4. """
  5. import macros
  6. type BlockLiteral[T] = object
  7. p: T
  8. proc p[T](a:int) = echo 1
  9. proc p[T](a:string) = echo "a"
  10. iterator arguments(formalParams: NimNode): NimNode =
  11. var iParam = 0
  12. for i in 1 ..< formalParams.len:
  13. let pp = formalParams[i]
  14. for j in 0 .. pp.len - 3:
  15. yield pp[j]
  16. inc iParam
  17. macro implementInvoke(T: typedesc): untyped =
  18. let t = getTypeImpl(T)[1]
  19. let call = newCall(newDotExpr(ident"blk", ident"p"))
  20. let params = copyNimTree(t[0])
  21. result = newProc(ident"invoke", body = call)
  22. # result[2] = newTree(nnkGenericParams,T)
  23. for n in arguments(params):
  24. call.add(n)
  25. params.insert(1, newIdentDefs(ident"blk", newTree(nnkBracketExpr, bindSym"BlockLiteral", T)))
  26. result.params = params
  27. proc getInvoke(T: typedesc) =
  28. implementInvoke(T)
  29. getInvoke(proc(a: int))