wrapnils

    Dark Mode
Search:
Group by:

This module allows chains of field-access and indexing where the LHS can be nil. This simplifies code by reducing need for if-else branches around intermediate values that may be nil.

Note: experimental module and relies on {.experimental: "dotOperators".} Unstable API.

Examples:

type
  Foo = ref object
    x1: string
    x2: Foo
    x3: ref int

var f: Foo
assert ?.f.x2.x1 == ""
var f2 = Foo(x1: "a")
f2.x2 = f2
assert ?.f2.x1 == "a"
assert ?.Foo(x1: "a").x1 == "a"
assert ?.(f2.x2.x2).x3[] == 0

Macros

macro `?.`(a: untyped): untyped
Transforms a into an expression that can be safely evaluated even in presence of intermediate nil pointers/references, in which case a default value is produced.   Source Edit

Templates

template `.`(a: Wrapnil; b): untyped
See top-level example.   Source Edit
template `[]`[I; ](a: Wrapnil; i: I): untyped
See top-level example.   Source Edit
template `[]`(a: Wrapnil): untyped
See top-level example.   Source Edit