twrapnils.nim 4.9 KB

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