Module tvix_eval::value::thunk

source ·
Expand description

This module implements the runtime representation of Thunks.

Thunks are a special kind of Nix value, similar to a 0-argument closure that yields some value. Thunks are used to implement the lazy evaluation behaviour of Nix:

Whenever the compiler determines that an expression should be evaluated lazily, it creates a thunk instead of compiling the expression value directly. At any point in the runtime where the actual value of a thunk is required, it is “forced”, meaning that the encompassing computation takes place and the thunk takes on its new value.

Thunks have interior mutability to be able to memoise their computation. Once a thunk is evaluated, its internal representation becomes the result of the expression. It is legal for the runtime to replace a thunk object directly with its value object, but when forcing a thunk, the runtime must mutate the memoisable slot.

Structs§

  • Internal representation of a suspended native thunk.
  • A thunk is created for any value which requires non-strict evaluation due to self-reference or lazy semantics (or both). Every reference cycle involving Values will contain at least one Thunk.
  • A wrapper type for tracking which thunks have already been seen in a context. This is necessary for printing and deeply forcing cyclic non-diverging data structures like rec { f = [ f ]; }. This is separate from the ThunkRepr::Blackhole mechanism, which detects diverging data structures like (rec { f = f; }).f.

Enums§

  • ThunkRepr 🔒
    Internal representation of the different states of a thunk.