tgenericparam.nim 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. block: # basic template generic parameter substitution
  2. block: # issue #13527
  3. template typeNameTempl[T](a: T): string = $T
  4. proc typeNameProc[T](a: T): string = $T
  5. doAssert typeNameTempl(1) == typeNameProc(1)
  6. doAssert typeNameTempl(true) == typeNameProc(true)
  7. doAssert typeNameTempl(1.0) == typeNameProc(1.0)
  8. doAssert typeNameTempl(1u8) == typeNameProc(1u8)
  9. template isDefault[T](a: T): bool = a == default(T)
  10. doAssert isDefault(0.0)
  11. block: # issue #17240
  12. func to(c: int, t: typedesc[float]): t = discard
  13. template converted[I, T](i: seq[I], t: typedesc[T]): seq[T] =
  14. var result = newSeq[T](2)
  15. result[0] = i[0].to(T)
  16. result
  17. doAssert newSeq[int](3).converted(float) == @[0.0, 0.0]
  18. block: # issue #6340
  19. type A[T] = object
  20. v: T
  21. proc foo(x: int): string = "int"
  22. proc foo(x: typedesc[int]): string = "typedesc[int]"
  23. template fooT(x: int): string = "int"
  24. template fooT(x: typedesc[int]): string = "typedesc[int]"
  25. proc foo[T](x: A[T]): (string, string) =
  26. (foo(T), fooT(T))
  27. template fooT[T](x: A[T]): (string, string) =
  28. (foo(T), fooT(T))
  29. var x: A[int]
  30. doAssert foo(x) == fooT(x)
  31. block: # issue #20033
  32. template run[T](): T = default(T)
  33. doAssert run[int]() == 0
  34. import options, tables, typetraits
  35. block: # complex cases of above with imports
  36. block: # issue #19576, complex case
  37. type RegistryKey = object
  38. key, val: string
  39. var regKey = @[RegistryKey(key: "abc", val: "def")]
  40. template findFirst[T](s: seq[T], pred: proc(x: T): bool): Option[T] =
  41. var res = none(T) # important line
  42. for x in s:
  43. if pred(x):
  44. res = some(x)
  45. break
  46. res
  47. proc getval(searchKey: string): Option[string] =
  48. let found = regKey.findFirst(proc (rk: RegistryKey): bool = rk.key == searchKey)
  49. if found.isNone: none(string)
  50. else: some(found.get().val)
  51. doAssert getval("strange") == none(string)
  52. doAssert getval("abc") == some("def")
  53. block: # issue #19076
  54. block: # case 1
  55. var tested: Table[string,int]
  56. template `[]`[V](t:Table[string,V],key:string):untyped =
  57. $V
  58. doAssert tested["abc"] == "int"
  59. template `{}`[V](t:Table[string,V],key:string):untyped =
  60. ($V, tables.`[]`(t, key))
  61. doAssert (try: tested{"abc"} except KeyError: ("not there", 123)) == ("not there", 123)
  62. tables.`[]=`(tested, "abc", 456)
  63. doAssert tested["abc"] == "int"
  64. doAssert tested{"abc"} == ("int", 456)
  65. block: # case 2
  66. type Foo[A,T] = object
  67. t:T
  68. proc init[A,T](f:type Foo,a:typedesc[A],t:T):Foo[A,T] = Foo[A,T](t:t)
  69. template fromOption[A](o:Option[A]):auto =
  70. when o.isSome:
  71. Foo.init(A,35)
  72. else:
  73. Foo.init(A,"hi")
  74. let op = fromOption(some(5))
  75. block: # issue #7461
  76. template p[T](): untyped = none(T)
  77. doAssert p[int]() == none(int)
  78. block: # issue #7995
  79. var res: string
  80. template copyRange[T](dest: seq[T], destOffset: int) =
  81. when supportsCopyMem(T):
  82. res = "A"
  83. else:
  84. res = "B"
  85. var a = @[1, 2, 3]
  86. copyRange(a, 0)
  87. doAssert res == "A"