template_various.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. discard """
  2. output: '''
  3. i2416
  4. 33
  5. foo55
  6. foo8.0
  7. fooaha
  8. bar7
  9. 10
  10. 4true
  11. 132
  12. '''
  13. """
  14. import macros
  15. import i2416
  16. i2416()
  17. import mcan_access_hidden_field
  18. var myfoo = createFoo(33, 44)
  19. echo myfoo.geta
  20. import mgensym_generic_cross_module
  21. foo(55)
  22. foo 8.0
  23. foo "aha"
  24. bar 7
  25. block generic_templates:
  26. type
  27. SomeObj = object of RootObj
  28. Foo[T, U] = object
  29. x: T
  30. y: U
  31. template someTemplate[T](): tuple[id: int32, obj: T] =
  32. var result: tuple[id: int32, obj: T] = (0'i32, T())
  33. result
  34. let ret = someTemplate[SomeObj]()
  35. # https://github.com/nim-lang/Nim/issues/7829
  36. proc inner[T](): int =
  37. discard
  38. template outer[A](): untyped =
  39. inner[A]()
  40. template outer[B](x: int): untyped =
  41. inner[B]()
  42. var i1 = outer[int]()
  43. var i2 = outer[int](i1)
  44. # https://github.com/nim-lang/Nim/issues/7883
  45. template t1[T: int|int64](s: string): T =
  46. var t: T
  47. t
  48. template t1[T: int|int64](x: int, s: string): T =
  49. var t: T
  50. t
  51. var i3: int = t1[int]("xx")
  52. block tgetast_typeliar:
  53. proc error(s: string) = quit s
  54. macro assertOrReturn2(condition: bool; message: string) =
  55. var line = condition.lineInfo()
  56. result = quote do:
  57. block:
  58. if not likely(`condition`):
  59. error("Assertion failed: " & $(`message`) & "\n" & `line`)
  60. return
  61. macro assertOrReturn(condition: bool): typed =
  62. var message : NimNode = newLit(condition.repr)
  63. # echo message
  64. result = getAst assertOrReturn2(condition, message)
  65. echo result.repr
  66. proc point(size: int16): tuple[x, y: int16] =
  67. # returns random point in square area with given `size`
  68. assertOrReturn size > 0
  69. type
  70. MyFloat = object
  71. val: float
  72. converter to_myfloat(x: float): MyFloat {.inline.} =
  73. MyFloat(val: x)
  74. block pattern_with_converter:
  75. proc `+`(x1, x2: MyFloat): MyFloat =
  76. MyFloat(val: x1.val + x2.val)
  77. proc `*`(x1, x2: MyFloat): MyFloat =
  78. MyFloat(val: x1.val * x2.val)
  79. template optMul{`*`(a, 2.0)}(a: MyFloat): MyFloat =
  80. a + a
  81. func floatMyFloat(x: MyFloat): MyFloat =
  82. result = x * 2.0
  83. func floatDouble(x: float): float =
  84. result = x * 2.0
  85. doAssert floatDouble(5) == 10.0
  86. block procparshadow:
  87. template something(name: untyped) =
  88. proc name(x: int) =
  89. var x = x # this one should not be rejected by the compiler (#5225)
  90. echo x
  91. something(what)
  92. what(10)
  93. # bug #4750
  94. type
  95. O = object
  96. i: int
  97. OP = ptr O
  98. template alf(p: pointer): untyped =
  99. cast[OP](p)
  100. proc t1(al: pointer) =
  101. var o = alf(al)
  102. proc t2(alf: pointer) =
  103. var x = alf
  104. var o = alf(x)
  105. block symchoicefield:
  106. type Foo = object
  107. len: int
  108. var f = Foo(len: 40)
  109. template getLen(f: Foo): int = f.len
  110. doAssert f.getLen == 40
  111. # This fails, because `len` gets the nkOpenSymChoice
  112. # treatment inside the template early pass and then
  113. # it can't be recognized as a field anymore
  114. import os, times
  115. include "sunset.nimf"
  116. block ttempl:
  117. const
  118. tabs = [["home", "index"],
  119. ["news", "news"],
  120. ["documentation", "documentation"],
  121. ["download", "download"],
  122. ["FAQ", "question"],
  123. ["links", "links"]]
  124. var i = 0
  125. for item in items(tabs):
  126. var content = $i
  127. var file: File
  128. if open(file, changeFileExt(item[1], "html"), fmWrite):
  129. write(file, sunsetTemplate(current=item[1], ticker="", content=content,
  130. tabs=tabs))
  131. close(file)
  132. else:
  133. write(stdout, "cannot open file for writing")
  134. inc(i)
  135. block ttempl4:
  136. template `:=`(name, val: untyped): typed =
  137. var name = val
  138. ha := 1 * 4
  139. hu := "ta-da" == "ta-da"
  140. echo ha, hu
  141. import mtempl5
  142. block ttempl5:
  143. echo templ()
  144. #bug #892
  145. proc parse_to_close(value: string, index: int, open='(', close=')'): int =
  146. discard
  147. # Call parse_to_close
  148. template get_next_ident: typed =
  149. discard "{something}".parse_to_close(0, open = '{', close = '}')
  150. get_next_ident()
  151. #identifier expected, but found '(open|open|open)'
  152. #bug #880 (also example in the manual!)
  153. template typedef(name: untyped, typ: typedesc) =
  154. type
  155. `T name` {.inject.} = typ
  156. `P name` {.inject.} = ref `T name`
  157. typedef(myint, int)
  158. var x: PMyInt
  159. block templreturntype:
  160. template `=~` (a: int, b: int): bool = false
  161. var foo = 2 =~ 3