Nil

If a reference points to nothing, it has the value nil. nil is the default value for all ref and ptr types. The nil value can also be used like any other literal value. For example, it can be used in an assignment like myRef = nil.

Dereferencing nil is an unrecoverable fatal runtime error (and not a panic).

A successful dereferencing operation p[] implies that p is not nil. This can be exploited by the implementation to optimize code like:

  1. p[].field = 3
  2. if p != nil:
  3. # if p were nil, `p[]` would have caused a crash already,
  4. # so we know `p` is always not nil here.
  5. action()

Into:

  1. p[].field = 3
  2. action()

Note: This is not comparable to C’s “undefined behavior” for dereferencing NULL pointers.