tgenerics_various.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. discard """
  2. output: '''
  3. we
  4. direct
  5. generic
  6. generic
  7. '''
  8. joinable: false
  9. """
  10. import algorithm, sugar, sequtils, typetraits, asyncdispatch
  11. block tconfusing_arrow:
  12. type Deck = object
  13. value: int
  14. proc sort(h: var seq[Deck]) =
  15. # works:
  16. h.sort(proc (x, y: Deck): auto =
  17. cmp(x.value, y.value))
  18. # fails:
  19. h.sort((x, y: Deck) => cmp(ord(x.value), ord(y.value)))
  20. var player: seq[Deck] = @[]
  21. player.sort()
  22. block tdictdestruct:
  23. type
  24. TDict[TK, TV] = object
  25. k: TK
  26. v: TV
  27. PDict[TK, TV] = ref TDict[TK, TV]
  28. proc fakeNew[T](x: var ref T, destroy: proc (a: ref T) {.nimcall.}) =
  29. discard
  30. proc destroyDict[TK, TV](a: PDict[TK, TV]) =
  31. return
  32. proc newDict[TK, TV](a: TK, b: TV): PDict[TK, TV] =
  33. fakeNew(result, destroyDict[TK, TV])
  34. # Problem: destroyDict is not instantiated when newDict is instantiated!
  35. discard newDict("a", "b")
  36. block tgenericdefaults:
  37. type
  38. TFoo[T, U, R = int] = object
  39. x: T
  40. y: U
  41. z: R
  42. TBar[T] = TFoo[T, array[4, T], T]
  43. var x1: TFoo[int, float]
  44. static:
  45. doAssert type(x1.x) is int
  46. doAssert type(x1.y) is float
  47. doAssert type(x1.z) is int
  48. var x2: TFoo[string, R = float, U = seq[int]]
  49. static:
  50. doAssert type(x2.x) is string
  51. doAssert type(x2.y) is seq[int]
  52. doAssert type(x2.z) is float
  53. var x3: TBar[float]
  54. static:
  55. doAssert type(x3.x) is float
  56. doAssert type(x3.y) is array[4, float]
  57. doAssert type(x3.z) is float
  58. block tprop:
  59. type
  60. TProperty[T] = object of RootObj
  61. getProc: proc(property: TProperty[T]): T {.nimcall.}
  62. setProc: proc(property: TProperty[T], value: T) {.nimcall.}
  63. value: T
  64. proc newProperty[T](value: RootObj): TProperty[T] =
  65. result.getProc = proc (property: TProperty[T]) =
  66. return property.value
  67. block trefs:
  68. type
  69. PA[T] = ref TA[T]
  70. TA[T] = object
  71. field: T
  72. var a: PA[string]
  73. new(a)
  74. a.field = "some string"
  75. proc someOther[T](len: string): seq[T] = discard
  76. proc someOther[T](len: int): seq[T] = echo "we"
  77. proc foo[T](x: T) =
  78. var s = someOther[T](34)
  79. #newSeq[T](34)
  80. foo 23
  81. when false:
  82. # Compiles unless you use var a: PA[string]
  83. type
  84. PA = ref TA
  85. TA[T] = object
  86. # Cannot instantiate:
  87. type
  88. TA[T] = object
  89. a: PA[T]
  90. PA[T] = ref TA[T]
  91. type
  92. PA[T] = ref TA[T]
  93. TA[T] = object
  94. block tmap_auto:
  95. let x = map(@[1, 2, 3], x => x+10)
  96. doAssert x == @[11, 12, 13]
  97. let y = map(@[(1,"a"), (2,"b"), (3,"c")], x => $x[0] & x[1])
  98. doAssert y == @["1a", "2b", "3c"]
  99. proc eatsTwoArgProc[T,S,U](a: T, b: S, f: proc(t: T, s: S): U): U =
  100. f(a,b)
  101. let z = eatsTwoArgProc(1, "a", (t,s) => $t & s)
  102. doAssert z == "1a"
  103. block tproctypecache_falsepositive:
  104. type
  105. Callback = proc() {.closure, gcsafe.}
  106. GameState = ref object
  107. playerChangeHandlers: seq[Callback]
  108. proc newGameState(): GameState =
  109. result = GameState(
  110. playerChangeHandlers: newSeq[Callback]() # this fails
  111. )
  112. block tptrinheritance:
  113. type NSPasteboardItem = ptr object
  114. type NSPasteboard = ptr object
  115. type NSArrayAbstract {.inheritable.} = ptr object
  116. type NSMutableArrayAbstract = ptr object of NSArrayAbstract
  117. type NSArray[T] = ptr object of NSArrayAbstract
  118. type NSMutableArray[T] = ptr object of NSArray[T]
  119. proc newMutableArrayAbstract(): NSMutableArrayAbstract = discard
  120. template newMutableArray(T: typedesc): NSMutableArray[T] =
  121. cast[NSMutableArray[T]](newMutableArrayAbstract())
  122. proc writeObjects(p: NSPasteboard, o: NSArray[NSPasteboardItem]) = discard
  123. let a = newMutableArray NSPasteboardItem
  124. var x: NSMutableArray[NSPasteboardItem]
  125. var y: NSArray[NSPasteboardItem] = x
  126. writeObjects(nil, a)
  127. block tsigtypeop:
  128. type Vec3[T] = array[3, T]
  129. proc foo(x: Vec3, y: Vec3.T, z: x.T): x.type.T =
  130. return 10
  131. var y: Vec3[int] = [1, 2, 3]
  132. var z: int = foo(y, 3, 4)
  133. block tvarargs_vs_generics:
  134. proc withDirectType(args: string) =
  135. echo "direct"
  136. proc withDirectType[T](arg: T) =
  137. echo "generic"
  138. proc withOpenArray(args: openArray[string]) =
  139. echo "openArray"
  140. proc withOpenArray[T](arg: T) =
  141. echo "generic"
  142. proc withVarargs(args: varargs[string]) =
  143. echo "varargs"
  144. proc withVarargs[T](arg: T) =
  145. echo "generic"
  146. withDirectType "string"
  147. withOpenArray "string"
  148. withVarargs "string"
  149. block:
  150. type
  151. Que[T] {.gcsafe.} = object
  152. x: T
  153. proc `=`[T](q: var Que[T]; x: Que[T]) =
  154. discard
  155. var x: Que[int]
  156. doAssert(x.x == 0)
  157. # bug #4466
  158. proc identity[T](t: T): T = t
  159. proc doSomething[A, B](t: tuple[a: A, b: B]) = discard
  160. discard identity((c: 1, d: 2))
  161. doSomething(identity((1, 2)))
  162. # bug #6231
  163. proc myProc[T, U](x: T or U) = discard
  164. myProc[int, string](x = 2)
  165. block: # issue #8390
  166. proc x[T:SomeFloat](q: openarray[T], y: T = 1): string =
  167. doAssert $q.type == $openarray[y.type]
  168. $y.type
  169. doAssert x(@[1.0]) == $1.0.type
  170. block: # issue #9381
  171. var evalCount {.compileTime.} = 0
  172. macro test(t: typed): untyped =
  173. inc evalCount
  174. t
  175. type GenericObj[T] = object
  176. f: test(T)
  177. var x: GenericObj[int]
  178. static: doAssert evalCount == 1