eve.result

eve.result — value-based success/failure results.

This module provides Success, the empty payload for operations that succeed without returning a value, and Result, a thin wrapper around eve_types, Either, eve.types.Either representing "either a recoverable error or a successful outcome".

Results are ordinary values. Construction, copying and inspection are allocation-free and non-throwing when the relevant payload operations are; attributes are inferred from those operations. This suits low-level @nogc nothrow APIs as well as general application code.

Semantics:

  • The success value is the first type argument and the recoverable

    error is the second, following the conventional Result!(Value, Error) order.

  • Payload types equal after Unqual normalization are rejected.

    Use distinct role wrappers for qualifier-distinguished types.

  • Construction accepts exactly one of the payload types; implicit

    conversions match neither constructor and must be made explicit by the caller.

  • A default-constructed Result holds E.init, so ok() is false.

    There is no separate unset alternative: this is indistinguishable from explicitly storing E.init, which may not be a meaningful domain error.

  • Accessing the inactive payload (value() on an error state, or

    error() on a success state) is a programming error. It terminates the process with a diagnostic on stderr — even in release builds — without allocating or throwing. Recoverable failures are represented by result values, never by this path.

  • ok() reports which alternative is active. It never inspects error

    codes, null references or payload contents to guess success.

The branch-based accessors (ok, value, error) are the primary ergonomic API while D lacks native pattern matching; exhaustive handling remains available through match. With selective imports, import match explicitly and name the result before matching. Should language-level matching arrive, removing the accessors would still be an explicit, compatibility-reviewed deprecation — not an automatic cleanup.

Example:

import eve.result : Result, Status, Success;

struct ParseError { string msg; size_t offset; }
struct Config { int value; }
struct UpdateError { enum Reason { NOT_FOUND, LOCKED } Reason reason; }

alias ParseResult = Result!(Config, ParseError);
alias UpdateStatus = Status!UpdateError;

ParseResult parseConfig(scope const(char)[] text) @safe pure nothrow @nogc {
   if (text.length == 0)
      return ParseResult(ParseError("empty input", 0));
   return ParseResult(Config(1));
}

// Caller-side handling:
auto result = parseConfig("");
if (result.ok()) {
   auto config = result.value();
} else {
   auto failure = result.error();
}

Design record: doc/EVE-RESULT.md in the repository documents the validated decisions behind this module.

tmpl match
alias Status

Types 3

structSuccess

Successful completion without a payload.

Used as the success payload of Status: Status!SomeError means "either plain success or SomeError".

Fields
private ubyte _reservedReserved storage. The DMD-frontend x86-64 ABI classifier (LDC and DMD) treats a union whose member is an empty struct as memory class, which forces `Status!E` to be returned through a hidden pointe...
structResult(V, E)

Either a recoverable error or a successful outcome.

Result privately stores tagged error/value alternatives and adds unambiguous construction, branch-based access and a fail-fast misuse contract on top of the positional storage of eve.types.Either. See the module documentation for the full semantics.

Parameters

Vthe success payload type; Unqual!V must differ from Unqual!E.
Ethe recoverable failure payload type.
Fields
private Either!(E, V) _e
private _valueMisuseMsgDiagnostic for success-payload access on an error alternative.
private _errorMisuseMsgDiagnostic for error-payload access on a success alternative.
Methods
Result opAssign()(ref Result rhs) refReplace the active alternative by delegating to the storage's assignment.
Result opAssign()(Result rhs) refDitto.
bool ok() const @safe pure nothrow @nogcReports whether the success alternative is active.
auto value()()Returns the success payload.
auto value()() constDitto.
auto error()()Returns the error payload.
auto error()() constDitto.
private void _misuse(string msg)() static @nogc nothrow @trustedTerminates the process after writing a compile-time-constant diagnostic.
Constructors
this(T error)Construct a `Result` holding an error payload.
this(T value)Construct a `Result` holding a success payload.
aliasStatus(E) = Result!(Success, E)

Result of an operation that succeeds without returning a value.

Parameters

Erecoverable failure payload type.

Templates 1

tmplmatch(handlers...)

Exhaustive handling over both alternatives, delegating to the positional Either.match core of eve.types.

This is a free UFCS function so handlers may capture their enclosing scope. Selective imports must include match: import eve.result: Result, match;. Alias one import if also using eve.types.match in the same scope. Attributes depend on the handlers; capturing delegates can require GC allocation.

Only lvalue results are accepted: store a temporary in a named variable before matching. A returned reference or closure borrowing a payload must not outlive that result, and the payload must not be replaced while the borrow is in use. Taking the result by reference prevents matching from destroying a by-value parameter before the caller can use a returned borrow; it does not enforce the lifetime of borrows returned by handlers.

Parameters

handlerscallables covering both payload types; exact single-parameter types are required (after Unqual normalization). The first matching handler is used; both alternatives must be covered.
resultlvalue result to inspect by reference without copying, preserving its qualifiers; it must remain alive while returned borrows are used.

Returns

the active handler's return value, or void for void handlers.

Throws

any exception thrown by the handler or its payload operations.
Functions
auto match(R)(ref R result) if (is(Unqual!R == Result!Args, Args...))

Ditto.