ddn.thunk

Synchronous, memoized lazy computations with shared identity.

Construct a Thunk with thunk, then call force() or `()` explicitly. Copies share one computation: the recipe runs at most once, on the forcing thread, and its outcome is retained. No implicit conversion, background execution, synchronization, invalidation or retry is provided. All handles to a computation must remain confined to one thread.

Recipes must be @safe callables without arguments. Values are returned by copy, not by reference; arrays and class references are not deep-copied. void recipes report successful completion through ddn.result.Success in tryForce(). Recoverable exceptions become ThunkFailure outcomes; Errors, including ThunkCycleError, are never converted to results.

The authoritative contract and reference-semantics rationale live in doc/DDN-THUNK.md; the short user guide is pages/thunk.md.

Types 5

structThunkFailure(T)

Recoverable failure returned by a thunk's tryForce().

The type parameter distinguishes the error role from every success payload, including successful Exception and ThunkFailure!U values, without changing the exact-type construction contract of ddn.result.

Parameters

TThe thunk's payload type, or void for a completion-only thunk.
Fields
Exception exceptionThe original exception; non-null for failures returned by `tryForce()`.
classInvalidThunkException : Exception

Invalid construction or forcing of an uninitialized thunk handle.

Constructors
this(string message, string file = __FILE__, size_t line = __LINE__)Describe the invalid operation. Params: message = Diagnostic text. file = Source file of the invalid operation. line = Source line of the invalid operation.

Recursive forcing of a computation whose recipe is still evaluating.

Cycles in this primitive are programming errors, not recoverable outcomes. Validate user-supplied graph data separately if cycles are expected input.

Constructors
this(string message, string file = __FILE__, size_t line = __LINE__)Describe the recursive forcing operation. Params: message = Diagnostic text. file = Source file of the recursive force. line = Source line of the recursive force.

A previous evaluation unwound without a recoverable outcome.

The computation is not retried. This is not a promise that arbitrary fatal runtime errors can be caught safely; it only defines state after unwinding.

Constructors
this(string message, string file = __FILE__, size_t line = __LINE__)Describe the aborted computation. Params: message = Diagnostic text. file = Source file of the force after abnormal exit. line = Source line of the force after abnormal exit.
structThunk(T)

Copyable handle to one synchronous, memoized computation.

Default construction produces an empty handle; forcing it throws InvalidThunkException. Rebinding one handle never changes a computation held by another copy. Handles are mutable: forcing through const, immutable or shared handles is deliberately unsupported.

Success and recoverable recipe/cache-construction failures are retained, including the original exception identity. Errors are never caught here; if evaluation unwinds without a recoverable outcome, the state is aborted and subsequent forcing throws ThunkAbortedError, without retrying. Ordinary result-copy failures occur after success publication and do not overwrite the cached outcome or rerun the recipe.

Parameters

TA copyable, top-level unqualified payload type, or void. Referenced data may be const or immutable, for example immutable(int)[].
Methods
T force()()Evaluate the recipe at most once and return its retained outcome.
T opCall()()Explicit call shorthand for `force()`. Returns: A copy of the cached value, or nothing for `Thunk!void`. Throws: The same failures as `force()`.
ThunkResult!T tryForce()()Force the computation, reporting recoverable exceptions as values.
Constructors
this(T delegate() @safe recipe)Construct a pending thunk without invoking the recipe. Params: recipe = An escaping, zero-argument `@safe` delegate. Throws: InvalidThunkException if the recipe is null.
this(T function() @safe recipe)Construct a pending thunk from an `@safe` free function. Params: recipe = A non-null, zero-argument `@safe` function pointer. Throws: InvalidThunkException if the recipe is null.

Functions 4

fnThunk!T thunk(T)(T delegate() @safe recipe)Construct a thunk, inferring its payload from a delegate. Params: recipe = Zero-argument `@safe` delegate, retained until evaluation. Returns: A pending Thunk sharing state with any future copies. ...
fnThunk!T thunk(T)(T function() @safe recipe)Construct a thunk, inferring its payload from a free function. Params: recipe = Zero-argument `@safe` function pointer. Returns: A pending Thunk. Throws: InvalidThunkException if the recipe is null.
fnauto lift1(alias fun, T)(Thunk!T input) if (!is(T == void))Defer a unary transformation of a thunk's value.
fnauto lift2(alias fun, T, U)(Thunk!T left, Thunk!U right) if (!is(T == void) && !is(U == void))Defer a binary transformation, forcing inputs in left-to-right order.

Templates 1

tmplThunkResult(T)

A thunk's recoverable outcome, using existing DDN result types.

Parameters

TThe payload type, or void for completion without a payload.