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
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
T | The thunk's payload type, or void for a completion-only thunk. |
Exception exceptionThe original exception; non-null for failures returned by `tryForce()`.Invalid construction or forcing of an uninitialized thunk handle.
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.
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.
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.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
T | A copyable, top-level unqualified payload type, or void. Referenced data may be const or immutable, for example immutable(int)[]. |
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.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
Thunk!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. ...Thunk!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.Templates 1
A thunk's recoverable outcome, using existing DDN result types.
Parameters
T | The payload type, or void for completion without a payload. |