tcallprecedence.nim 850 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import macros
  2. {.experimental: "dotOperators".}
  3. {.experimental: "callOperator".}
  4. block:
  5. template `.()`(foo: int, args: varargs[untyped]): untyped {.used.} =
  6. ".()"
  7. template `()`(foo: int, args: varargs[untyped]): untyped =
  8. "()"
  9. let a = (b: 1)
  10. let c = 3
  11. doAssert a.b(c) == "()"
  12. doAssert not compiles(a(c))
  13. doAssert (a.b)(c) == "()"
  14. macro `()`(args: varargs[typed]): untyped =
  15. result = newLit("() " & args.treeRepr)
  16. macro `.`(args: varargs[typed]): untyped =
  17. result = newLit(". " & args.treeRepr)
  18. block:
  19. let a = 1
  20. let b = 2
  21. doAssert a.b == `()`(b, a)
  22. block:
  23. let a = 1
  24. proc b(): int {.used.} = 2
  25. doAssert a.b == `.`(a, b)
  26. block:
  27. let a = 1
  28. proc b(x: int): int = x + 1
  29. let c = 3
  30. doAssert a.b(c) == `.`(a, b, c)
  31. doAssert a(b) == `()`(a, b)
  32. doAssert (a.b)(c) == `()`(a.b, c)
  33. doAssert a.b == b(a)