tbind.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. discard """
  2. output: '''
  3. 3
  4. 1
  5. 1
  6. 1
  7. 5
  8. '''
  9. """
  10. block tbind:
  11. # Test the new ``bind`` keyword for templates
  12. proc p1(x: int8, y: int): int = return x + y
  13. template tempBind(x, y): untyped =
  14. bind p1
  15. p1(x, y)
  16. proc p1(x: int, y: int8): int = return x - y
  17. # This is tricky: the call to ``p1(1'i8, 2'i8)`` should not fail in line 6,
  18. # because it is not ambiguous there. But it is ambiguous after line 8.
  19. echo tempBind(1'i8, 2'i8) #OUT 3
  20. import mbind3
  21. echo genId() #OUT 1
  22. import strtabs
  23. block tbinoverload:
  24. template t() =
  25. block:
  26. bind newStringTable
  27. discard {"Content-Type": "text/html"}.newStringTable()
  28. discard {:}.newStringTable
  29. #discard {"Content-Type": "text/html"}.newStringTable()
  30. t()
  31. block tmixin:
  32. type
  33. TFoo1 = object of RootObj
  34. v: int
  35. TFoo2 = object of TFoo1
  36. v2: int
  37. proc test(f: TFoo1) =
  38. echo "1"
  39. proc Foo[T](f: T) =
  40. mixin test
  41. test(f)
  42. var
  43. a: TFoo1
  44. b: TFoo2
  45. proc test(f: TFoo2) =
  46. echo "2"
  47. Foo(a)
  48. Foo(b)
  49. # issue #11811
  50. proc p(a : int) =
  51. echo a
  52. proc printVar*[T:int|float|string](a : T) =
  53. bind p
  54. p(a)
  55. printVar(5)