tisopr.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. discard """
  2. output: '''true true false yes
  3. false
  4. true
  5. false
  6. true
  7. true
  8. yes'''
  9. """
  10. proc IsVoid[T](): string =
  11. when T is void:
  12. result = "yes"
  13. else:
  14. result = "no"
  15. const x = int is int
  16. echo x, " ", float is float, " ", float is string, " ", IsVoid[void]()
  17. template yes(e): void =
  18. static: assert e
  19. template no(e): void =
  20. static: assert(not e)
  21. when false:
  22. var s = @[1, 2, 3]
  23. yes s.items is iterator
  24. no s.items is proc
  25. yes s.items is iterator: int
  26. no s.items is iterator: float
  27. yes s.items is iterator: TNumber
  28. no s.items is iterator: object
  29. type
  30. Iter[T] = iterator: T
  31. yes s.items is Iter[TNumber]
  32. no s.items is Iter[float]
  33. type
  34. Foo[N: static[int], T] = object
  35. field: array[1..N, T]
  36. Bar[T] = Foo[4, T]
  37. Baz[N: static[int]] = Foo[N, float]
  38. no Foo[2, float] is Foo[3, float]
  39. no Foo[2, float] is Foo[2, int]
  40. yes Foo[4, string] is Foo[4, string]
  41. yes Bar[int] is Foo[4, int]
  42. yes Foo[4, int] is Bar[int]
  43. no Foo[4, int] is Baz[4]
  44. yes Foo[4, float] is Baz[4]
  45. # bug #2505
  46. echo(8'i8 is int32)
  47. # bug #1853
  48. type SeqOrSet[E] = seq[E] or set[E]
  49. type SeqOfInt = seq[int]
  50. type SeqOrSetOfInt = SeqOrSet[int]
  51. # This prints "true", as expected. Previously "false" was returned and that
  52. # seemed less correct that (1) printing "true" or (2) raising a compiler error.
  53. echo seq is SeqOrSet
  54. # This prints "false", as expected.
  55. echo seq is SeqOrSetOfInt
  56. # This prints "true", as expected.
  57. echo SeqOfInt is SeqOrSet
  58. # This causes an internal error (filename: compiler/semtypes.nim, line: 685).
  59. echo SeqOfInt is SeqOrSetOfInt
  60. # bug #2522
  61. proc test[T](x: T) =
  62. when T is typedesc:
  63. echo "yes"
  64. else:
  65. echo "no"
  66. test(7)
  67. block:
  68. # bug #13066
  69. type Bar[T1,T2] = object
  70. type Foo[T1,T2] = object
  71. type Foo2 = Foo
  72. doAssert Foo2 is Foo
  73. doAssert Foo is Foo2
  74. doAssert Foo is Foo
  75. doAssert Foo2 is Foo2
  76. doAssert Foo2 isnot Bar
  77. doAssert Foo[int,float] is Foo2[int,float]
  78. # other
  79. doAssert Foo[int,float] isnot Foo2[float,float]
  80. doAssert Foo[int,float] is Foo2
  81. doAssert Foo[int,float|int] is Foo2
  82. doAssert Foo2[int,float|int] is Foo
  83. doAssert Foo2[int,float|int] isnot Bar
  84. doAssert int is (int|float)
  85. block:
  86. # Slice[T] as static type issue
  87. type
  88. MyEnum = enum
  89. x1, x2, x3, x4, x5, x6
  90. proc enumGen[T: enum](s: static[Slice[T]]) =
  91. doAssert($s.a & " " & $s.b == "x1 x3")
  92. enumGen(x1..x3)
  93. block:
  94. # issue #11142
  95. type
  96. MyObjParam[N: static int] = object
  97. x: int
  98. MyObj[P: static MyObjParam] = object
  99. y: int
  100. const P = MyObjParam[256](x: 2)
  101. let Q = MyObj[P](y: 2)
  102. doAssert($Q == "(y: 2)")
  103. block: # previously tisop.nim
  104. type
  105. TRecord = (tuple) or (object)
  106. TFoo[T, U] = object
  107. x: int
  108. when T is string:
  109. y: float
  110. else:
  111. y: string
  112. when U is TRecord:
  113. z: float
  114. E = enum A, B, C
  115. template m(t: typedesc): typedesc =
  116. when t is enum:
  117. string
  118. else:
  119. int
  120. var f: TFoo[int, int]
  121. static: doAssert(typeof(f.y) is string)
  122. when compiles(f.z):
  123. {.error: "Foo should not have a `z` field".}
  124. proc p(a, b: auto) =
  125. when typeof(a) is int:
  126. static: doAssert false
  127. var f: TFoo[m(a.typeof), b.typeof]
  128. static:
  129. doAssert f.x.typeof is int
  130. doAssert f.y.typeof is float
  131. doAssert f.z.typeof is float
  132. p(A, f)