Skip to content

Commit

Permalink
for now, wrapnil, isValid, unwrap are not exported
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jan 18, 2020
1 parent b3895a2 commit 205a7dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/std/wrapnils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ runnableExamples:
# which is just sugar for the following:
assert f2.x2.x2.wrapnil.x3[].unwrap == 0

type Wrapnil*[T] = object
type Wrapnil[T] = object
valueImpl: T
validImpl: bool

proc wrapnil*[T](a: T): Wrapnil[T] =
proc wrapnil[T](a: T): Wrapnil[T] =
## See top-level example.
Wrapnil[T](valueImpl: a, validImpl: true)

template unwrap*(a: Wrapnil): untyped =
template unwrap(a: Wrapnil): untyped =
## See top-level example.
a.valueImpl

Expand All @@ -62,7 +62,7 @@ template `.`*(a: Wrapnil, b): untyped =

{.pop.}

proc isValid*(a: Wrapnil): bool =
proc isValid(a: Wrapnil): bool =
## Returns true if `a` didn't contain intermediate `nil` values (note that
## `a.valueImpl` itself can be nil even in that case)
a.validImpl
Expand Down
17 changes: 10 additions & 7 deletions tests/stdlib/twrapnils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import std/wrapnils

const wrapnilExtendedExports = declared(wrapnil)
# for now, wrapnil, isValid, unwrap are not exported

proc checkNotZero(x: float): float =
doAssert x != 0
x
Expand Down Expand Up @@ -47,9 +50,12 @@ proc main() =
doAssert ?.a3.x2.x2.x5.len == 0
doAssert a3.x2.x2.x3.len == 3

# example calling wrapnil directly, with and without unwrap
doAssert a3.wrapnil.x2.x2.x3.len == wrapnil(3)
doAssert a3.wrapnil.x2.x2.x3.len.unwrap == 3
when wrapnilExtendedExports:
# example calling wrapnil directly, with and without unwrap
doAssert a3.wrapnil.x2.x2.x3.len == wrapnil(3)
doAssert a3.wrapnil.x2.x2.x3.len.unwrap == 3
doAssert a2.wrapnil.x4.isValid
doAssert not a.wrapnil.x4.isValid

doAssert ?.a.x2.x2.x3[1] == default(char)
# here we only apply wrapnil around gook.foo, not gook (and assume gook is not nil)
Expand All @@ -62,11 +68,8 @@ proc main() =
# shows that checkNotZero won't be called if a nil is found earlier in chain
doAssert ?.a.x1.checkNotZero == 0.0

doAssert a2.wrapnil.x4.isValid
doAssert not a.wrapnil.x4.isValid

# checks that a chain without nil but with an empty seq still throws IndexError
doAssertRaises(IndexError): discard ?.a2.wrapnil.x8[3]
doAssertRaises(IndexError): discard ?.a2.x8[3]

# make sure no double evaluation bug
doAssert witness == 0
Expand Down

0 comments on commit 205a7dd

Please sign in to comment.