twrapnils.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import std/wrapnils
  2. from std/options import get, isSome
  3. import std/assertions
  4. proc checkNotZero(x: float): float =
  5. doAssert x != 0
  6. x
  7. proc main() =
  8. var witness = 0
  9. block:
  10. type Bar = object
  11. b1: int
  12. b2: ptr string
  13. type Foo = ref object
  14. x1: float
  15. x2: Foo
  16. x3: string
  17. x4: Bar
  18. x5: seq[int]
  19. x6: ptr Bar
  20. x7: array[2, string]
  21. x8: seq[int]
  22. x9: ref Bar
  23. type Goo = ref object
  24. foo: Foo
  25. proc fun(a: Bar): auto = a.b2
  26. var a: Foo
  27. var x6: ptr Bar
  28. when nimvm: discard # pending https://github.com/timotheecour/Nim/issues/568
  29. else:
  30. x6 = create(Bar)
  31. x6.b1 = 42
  32. var a2 = Foo(x1: 1.0, x5: @[10, 11], x6: x6)
  33. var a3 = Foo(x1: 1.2, x3: "abc")
  34. a3.x2 = a3
  35. var goo = Goo(foo: a)
  36. proc initFoo(x1: float): auto =
  37. witness.inc
  38. result = Foo(x1: x1)
  39. doAssert ?.a.x2.x2.x1 == 0.0
  40. doAssert ?.a3.x2.x2.x1 == 1.2
  41. doAssert ?.a3.x2.x2.x3[1] == 'b'
  42. doAssert ?.a3.x2.x2.x5.len == 0
  43. doAssert a3.x2.x2.x3.len == 3
  44. doAssert ?.a.x2.x2.x3[1] == default(char)
  45. # here we only apply wrapnil around goo.foo, not goo (and assume goo is not nil)
  46. doAssert ?.(goo.foo).x2.x2.x1 == 0.0
  47. when nimvm: discard
  48. else:
  49. doAssert ?.a2.x6[] == Bar(b1: 42) # deref for ptr Bar
  50. doAssert ?.a2.x1.checkNotZero == 1.0
  51. doAssert a == nil
  52. # shows that checkNotZero won't be called if a nil is found earlier in chain
  53. doAssert ?.a.x1.checkNotZero == 0.0
  54. when nimvm: discard
  55. else:
  56. # checks that a chain without nil but with an empty seq still raises
  57. doAssertRaises(IndexDefect): discard ?.a2.x8[3]
  58. # make sure no double evaluation bug
  59. doAssert witness == 0
  60. doAssert ?.initFoo(1.3).x1 == 1.3
  61. doAssert witness == 1
  62. # here, it's used twice, to deref `ref Bar` and then `ptr string`
  63. doAssert ?.a.x9[].fun[] == ""
  64. block: # `??.`
  65. doAssert (??.a3.x2.x2.x3.len).get == 3
  66. doAssert (??.a2.x4).isSome
  67. doAssert not (??.a.x4).isSome
  68. block:
  69. type
  70. A = object
  71. b: B
  72. B = object
  73. c: C
  74. C = object
  75. d: D
  76. D = ref object
  77. e: E
  78. e2: array[2, E]
  79. e3: seq[E]
  80. d3: D
  81. i4: int
  82. E = object
  83. f: int
  84. d2: D
  85. proc identity[T](a: T): T = a
  86. proc identity2[T](a: T, ignore: int): T = a
  87. var a: A
  88. doAssert ?.a.b.c.d.e.f == 0
  89. doAssert ?.a.b.c.d.e.d2.d3[].d3.e.d2.e.f == 0
  90. doAssert ?.a.b.c.d.d3[].e.f == 0
  91. doAssert ?.a.b.c.d.e2[0].d2.e3[0].f == 0
  92. doAssert ?.a == A.default
  93. doAssert ?.a.b.c.d.e == E.default
  94. doAssert ?.a.b.c.d.e.d2 == nil
  95. doAssert ?.a.identity.b.c.identity2(12).d.d3.e.f == 0
  96. doAssert ?.a.b.c.d.d3.e2[0].f == 0
  97. a.b.c.d = D()
  98. a.b.c.d.d3 = a.b.c.d
  99. a.b.c.d.e2[0].f = 5
  100. doAssert ?.a.b.c.d.d3.e2[0].f == 5
  101. var d: D = nil
  102. doAssert ?.d.identity.i4 == 0
  103. doAssert ?.d.i4.identity == 0
  104. block: # case objects
  105. type
  106. Kind = enum k0, k1, k2
  107. V = object
  108. case kind: Kind
  109. of k0:
  110. x0: int
  111. of k1:
  112. x1: int
  113. of k2:
  114. x2: int
  115. A = object
  116. v0: V
  117. block:
  118. var a = V(kind: k0, x0: 3)
  119. doAssert ?.a.x0 == 3
  120. doAssert ?.a.x1 == 0
  121. a = V(kind: k1, x1: 5)
  122. doAssert ?.a.x0 == 0
  123. doAssert ?.a.x1 == 5
  124. block:
  125. var a = A(v0: V(kind: k0, x0: 10))
  126. doAssert ?.a.v0.x0 == 10
  127. doAssert ?.a.v0.x1 == 0
  128. a.v0 = V(kind: k2, x2: 8)
  129. doAssert ?.a.v0.x0 == 0
  130. doAssert ?.a.v0.x1 == 0
  131. doAssert ?.a.v0.x2 == 8
  132. block: # `nnkCall`
  133. type
  134. A = object
  135. a0: int
  136. d: D
  137. D = ref object
  138. i4: int
  139. proc identity[T](a: T): T = a
  140. var d: D = nil
  141. doAssert ?.d.i4.identity == 0
  142. doAssert ?.identity(?.d.i4) == 0
  143. doAssert ?.identity(d.i4) == 0
  144. doAssert ?.identity(d) == nil
  145. doAssert ?.identity(d[]) == default(typeof(d[]))
  146. doAssert ?.identity(d[]).i4 == 0
  147. var a: A
  148. doAssert ?.identity(a) == default(A)
  149. doAssert ?.identity(a.a0) == 0
  150. doAssert ?.identity(a.d) == nil
  151. doAssert ?.identity(a.d.i4) == 0
  152. block: # lvalue semantic propagation
  153. type
  154. A = ref object
  155. a0: A
  156. a1: seq[A]
  157. a2: int
  158. B = object
  159. b0: int
  160. case cond: bool
  161. of false: discard
  162. of true:
  163. b1: float
  164. block:
  165. var a: A
  166. doAssert ?.a.a0.a1[0].a2.addr == nil
  167. a = A(a2: 3)
  168. doAssert ?.a.a0.a1[0].a2.addr == nil
  169. a.a0 = a
  170. a.a1 = @[a]
  171. let p = ?.a.a0.a1[0].a2.addr
  172. doAssert p != nil
  173. p[] = 5
  174. doAssert a.a2 == 5
  175. block:
  176. var b = B(cond: false, b0: 3)
  177. let p = ?.b.b1.addr
  178. doAssert p == nil
  179. b = B(cond: true, b1: 4.5)
  180. let p2 = ?.b.b1.addr
  181. doAssert p2 != nil
  182. p2[] = 4.6
  183. doAssert b.b1 == 4.6
  184. # useful pattern, impossible with Options
  185. if (let p3 = ?.b.b1.addr; p3 != nil):
  186. p3[] = 4.7
  187. doAssert b.b1 == 4.7
  188. main()
  189. static: main()