tgeneric0.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. discard """
  2. output: '''
  3. 100
  4. 0
  5. float32
  6. float32
  7. (name: "Resource 1", readers: ..., writers: ...)
  8. '''
  9. """
  10. import std/tables
  11. block tgeneric0:
  12. type
  13. TX = Table[string, int]
  14. proc foo(models: seq[Table[string, float]]): seq[float] =
  15. result = @[]
  16. for model in models.items:
  17. result.add model["foobar"]
  18. # bug #686
  19. type TType[T; A] = array[A, T]
  20. proc foo[T](p: TType[T, range[0..1]]) =
  21. echo "foo"
  22. proc foo[T](p: TType[T, range[0..2]]) =
  23. echo "bar"
  24. #bug #1366
  25. proc reversed(x: auto) =
  26. for i in countdown(x.low, x.high):
  27. echo i
  28. reversed(@[-19, 7, -4, 6])
  29. block tgeneric1:
  30. type
  31. TNode[T] = tuple[priority: int, data: T]
  32. TBinHeap[T] = object
  33. heap: seq[TNode[T]]
  34. last: int
  35. PBinHeap[T] = ref TBinHeap[T]
  36. proc newBinHeap[T](heap: var PBinHeap[T], size: int) =
  37. new(heap)
  38. heap.last = 0
  39. newSeq(heap.heap, size)
  40. #newSeq(heap.seq, size)
  41. proc parent(elem: int): int {.inline.} =
  42. return (elem-1) div 2
  43. proc siftUp[T](heap: PBinHeap[T], elem: int) =
  44. var idx = elem
  45. while idx != 0:
  46. var p = parent(idx)
  47. if heap.heap[idx].priority < heap.heap[p].priority:
  48. swap(heap.heap[idx], heap.heap[p])
  49. idx = p
  50. else:
  51. break
  52. proc add[T](heap: PBinHeap[T], priority: int, data: T) =
  53. var node: TNode[T]
  54. node.priority = priority
  55. node.data = data
  56. heap.heap[heap.last] = node
  57. siftUp(heap, heap.last)
  58. inc(heap.last)
  59. proc print[T](heap: PBinHeap[T]) =
  60. for i in countup(0, heap.last):
  61. stdout.write($heap.heap[i].data, "\n")
  62. var heap: PBinHeap[int]
  63. newBinHeap(heap, 256)
  64. add(heap, 1, 100)
  65. print(heap)
  66. block tgeneric2:
  67. type
  68. TX = Table[string, int]
  69. proc foo(models: seq[TX]): seq[int] =
  70. result = @[]
  71. for model in models.items:
  72. result.add model["foobar"]
  73. type
  74. Obj = object
  75. field: Table[string, string]
  76. var t: Obj
  77. discard initTable[type(t.field), string]()
  78. block tgeneric4:
  79. type
  80. TIDGen[A: Ordinal] = object
  81. next: A
  82. free: seq[A]
  83. proc newIDGen[A]: TIDGen[A] =
  84. newSeq result.free, 0
  85. var x = newIDGen[int]()
  86. block tgeneric5:
  87. # bug #12528
  88. proc foo[T](a: T; b: T) =
  89. echo T
  90. foo(0.0'f32, 0.0)
  91. proc bar[T](a: T; b: T = 0.0) =
  92. echo T
  93. bar(0.0'f32)
  94. # bug #13378
  95. type
  96. Resource = ref object of RootObj
  97. name: string
  98. readers, writers: seq[RenderTask]
  99. RenderTask = ref object
  100. name: string
  101. var res = Resource(name: "Resource 1")
  102. (proc (r: typeof(res)) =
  103. echo r[])(res)
  104. # bug #4061
  105. type List[T] = object
  106. e: T
  107. n: ptr List[T]
  108. proc zip*[T,U](xs: List[T], ys: List[U]): List[(T,U)] = discard
  109. proc unzip*[T,U](xs: List[tuple[t: T, u: U]]): (List[T], List[U]) = discard
  110. proc unzip2*[T,U](xs: List[(T,U)]): (List[T], List[U]) = discard
  111. type
  112. AtomicType = pointer|ptr|int
  113. Atomic[T: AtomicType] = distinct T
  114. Block[T: AtomicType] = object
  115. AtomicContainer[T: AtomicType] = object
  116. b: Atomic[ptr Block[T]]
  117. # bug #8295
  118. var x = AtomicContainer[int]()
  119. doAssert (ptr Block[int])(x.b) == nil
  120. # bug #23233
  121. type
  122. JsonObjectType*[T: string or uint64] = Table[string, JsonValueRef[T]]
  123. JsonValueRef*[T: string or uint64] = object
  124. objVal*: JsonObjectType[T]
  125. proc scanValue[K](val: var K) =
  126. var map: JsonObjectType[K.T]
  127. var newVal: K
  128. map["one"] = newVal
  129. block:
  130. var a: JsonValueRef[uint64]
  131. scanValue(a)
  132. var b: JsonValueRef[string]
  133. scanValue(b)
  134. block: # bug #21347
  135. type K[T] = object
  136. template s[T]() = discard
  137. proc b1(n: bool | bool) = s[K[K[int]]]()
  138. proc b2(n: bool) = s[K[K[int]]]()
  139. b1(false) # Error: 's' has unspecified generic parameters
  140. b2(false) # Builds, on its own