Skip to content

Commit ae9ba26

Browse files
timotheecourdom96
andauthored
clarify docs for wrapnils (#16929)
Co-authored-by: Dominik Picheta <[email protected]>
1 parent 2a60974 commit ae9ba26

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/std/wrapnils.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ runnableExamples:
1919
assert ?.f2.x1 == "a" # same as f2.x1 (no nil LHS in this chain)
2020
assert ?.Foo(x1: "a").x1 == "a" # can use constructor inside
2121

22-
# when you know a sub-expression is not nil, you can scope it as follows:
23-
assert ?.(f2.x2.x2).x3[] == 0 # because `f` is nil
22+
# when you know a sub-expression doesn't involve a `nil` (e.g. `f2.x2.x2`),
23+
# you can scope it as follows:
24+
assert ?.(f2.x2.x2).x3[] == 0
25+
26+
assert (?.f2.x2.x2).x3 == nil # this terminates ?. early
27+
import segfaults # enable `NilAccessDefect` exceptions
28+
doAssertRaises(NilAccessDefect): echo (?.f2.x2.x2).x3[]
2429

2530
type Wrapnil[T] = object
2631
valueImpl: T

0 commit comments

Comments
 (0)