wrapnils.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ## This module allows chains of field-access and indexing where the LHS can be nil.
  2. ## This simplifies code by reducing need for if-else branches around intermediate values
  3. ## that maybe be nil.
  4. ##
  5. ## Note: experimental module and relies on {.experimental: "dotOperators".}
  6. ## Unstable API.
  7. runnableExamples:
  8. type Foo = ref object
  9. x1: string
  10. x2: Foo
  11. x3: ref int
  12. var f: Foo
  13. assert ?.f.x2.x1 == "" # returns default value since `f` is nil
  14. var f2 = Foo(x1: "a")
  15. f2.x2 = f2
  16. assert ?.f2.x1 == "a" # same as f2.x1 (no nil LHS in this chain)
  17. assert ?.Foo(x1: "a").x1 == "a" # can use constructor inside
  18. # when you know a sub-expression is not nil, you can scope it as follows:
  19. assert ?.(f2.x2.x2).x3[] == 0 # because `f` is nil
  20. type Wrapnil[T] = object
  21. valueImpl: T
  22. validImpl: bool
  23. proc wrapnil[T](a: T): Wrapnil[T] =
  24. ## See top-level example.
  25. Wrapnil[T](valueImpl: a, validImpl: true)
  26. template unwrap(a: Wrapnil): untyped =
  27. ## See top-level example.
  28. a.valueImpl
  29. {.push experimental: "dotOperators".}
  30. template `.`*(a: Wrapnil, b): untyped =
  31. ## See top-level example.
  32. let a1 = a # to avoid double evaluations
  33. let a2 = a1.valueImpl
  34. type T = Wrapnil[type(a2.b)]
  35. if a1.validImpl:
  36. when type(a2) is ref|ptr:
  37. if a2 == nil:
  38. default(T)
  39. else:
  40. wrapnil(a2.b)
  41. else:
  42. wrapnil(a2.b)
  43. else:
  44. # nil is "sticky"; this is needed, see tests
  45. default(T)
  46. {.pop.}
  47. proc isValid(a: Wrapnil): bool =
  48. ## Returns true if `a` didn't contain intermediate `nil` values (note that
  49. ## `a.valueImpl` itself can be nil even in that case)
  50. a.validImpl
  51. template `[]`*[I](a: Wrapnil, i: I): untyped =
  52. ## See top-level example.
  53. let a1 = a # to avoid double evaluations
  54. if a1.validImpl:
  55. # correctly will raise IndexDefect if a is valid but wraps an empty container
  56. wrapnil(a1.valueImpl[i])
  57. else:
  58. default(Wrapnil[type(a1.valueImpl[i])])
  59. template `[]`*(a: Wrapnil): untyped =
  60. ## See top-level example.
  61. let a1 = a # to avoid double evaluations
  62. let a2 = a1.valueImpl
  63. type T = Wrapnil[type(a2[])]
  64. if a1.validImpl:
  65. if a2 == nil:
  66. default(T)
  67. else:
  68. wrapnil(a2[])
  69. else:
  70. default(T)
  71. import std/macros
  72. proc replace(n: NimNode): NimNode =
  73. if n.kind == nnkPar:
  74. doAssert n.len == 1
  75. newCall(bindSym"wrapnil", n[0])
  76. elif n.kind in {nnkCall, nnkObjConstr}:
  77. newCall(bindSym"wrapnil", n)
  78. elif n.len == 0:
  79. newCall(bindSym"wrapnil", n)
  80. else:
  81. n[0] = replace(n[0])
  82. n
  83. macro `?.`*(a: untyped): untyped =
  84. ## Transforms `a` into an expression that can be safely evaluated even in
  85. ## presence of intermediate nil pointers/references, in which case a default
  86. ## value is produced.
  87. #[
  88. Using a template like this wouldn't work:
  89. template `?.`*(a: untyped): untyped = wrapnil(a)[]
  90. ]#
  91. result = replace(a)
  92. result = quote do:
  93. `result`.valueImpl