Result

public enum Result[T, E]: not Copyable where T: not Static

Cases

case Err(E)

The failure branch — wraps an E.

case Ok(T)

The success branch — wraps a T.

Methods

public func andThen[U]((T) -> Result[U, E]) -> Result[U, E]

Alias for flatMap — chains a fallible step onto an Ok branch. Reads more naturally in long pipelines (parseInput().andThen(validate).andThen(persist)).

public func andValue[U](Result[U, E]) -> Result[U, E]

Returns other when self is Ok, otherwise propagates the existing Err. Named andValue (not and) because and is a reserved keyword.

public static func err(E) -> Result[T, E]

Wraps error in .Err. Useful when constructing a Result from a known error in non-promotion contexts.

public func err() -> Optional[E]

Discards the success value, returning Some(error) for .Err and None for .Ok. Mirror of ok().

public func flatMap[U]((T) -> Result[U, E]) -> Result[U, E]

Monadic bind on the success branch — apply a transform that itself returns a Result, without nesting.

public func flatMapErr[F]((E) -> Result[T, F]) -> Result[T, F]

Monadic bind on the error branch — apply a recovery function that itself returns a Result, without nesting. Mirror of flatMap.

public func isErr() -> Bool

True when this is .Err. Complement of isOk.

public func isOk() -> Bool

True when this is .Ok. Cheap discriminator-only check.

Examples

Ok(42).isOk(); // true Err("oops").isOk(); // false
public func iter() -> ResultIterator[T, E]

Returns a ResultIterator yielding the success value (one element for .Ok, zero for .Err). Lets a Result plug into iterator pipelines that only care about the happy path.

public func map[U]((T) -> U) -> Result[U, E]

Functor map on the success branch. .Err passes through unchanged.

Examples

Ok(2).map { it * 2 }; // Ok(4) Err("oops").map { it * 2 }; // Err("oops")
public func mapErr[F]((E) -> F) -> Result[T, F]

Functor map on the error branch — typically used to widen a specific error type into a more general one.

Examples

parse(s).mapErr { AppError.Parse(it) };
public static func ok(T) -> Result[T, E]

Wraps value in .Ok. Rarely needed in practice — FromValue promotes bare values where the context expects a Result.

public func ok() -> Optional[T]

Discards the error, returning Some(value) for .Ok and None for .Err.

public func orElse[F]((E) -> Result[T, F]) -> Result[T, F]

Returns self when Ok, otherwise calls alternative(error). Use this for recovery logic that depends on which error occurred — e.g. retrying on a transient error but bubbling a permanent one.

public func orValue(Result[T, E]) -> Result[T, E]

Returns self when Ok, otherwise returns other. Named orValue because or is a reserved keyword.

public func unwrap() -> T

Returns the success value, panicking if Err. Use unwrap(or:) or pattern matching unless you can prove the result is Ok.

Errors

Panics with "called unwrap() on Err" when invoked on .Err.

public func unwrap(or: T) -> T

Returns the success value or default on Err. default is always evaluated — use unwrap(orElse:) if computing it is expensive or depends on the error.

public func unwrap(orElse: (E) -> T) -> T

Like unwrap(or:), but defaultFn receives the error value and is only invoked on Err. Useful when the recovery value depends on what went wrong.

public func unwrapErr() -> E

Returns the error value, panicking if Ok. Mostly used in tests to assert that a call failed.

Errors

Panics with "called unwrapErr() on Ok" when invoked on .Ok.

ImplementsTryable

Associated Types

type Output = T
type Residual = E

Methods

public consuming func tryExtract() -> ControlFlow[T, E]

Drives tryContinue(value) for .Ok, Break(error) for .Err. Defined inline because Tryable is declared in the enum's conformance list above.

ImplementsExitable

Methods

consuming func report() -> ExitCode

ImplementsFromResidual

Methods

public static func fromResidual(consuming E) -> Result[T, E]

Builds .Err(residual) from the residual produced by a try short-circuit.

ImplementsFromValue

Methods

public static func from(consuming T) -> Result[T, E]

Wraps value in .Ok. Called by the compiler at the promotion site, not usually by user code. consuming so value is moved into .Ok (no clone-and-leak of a borrowed original).

ImplementsEquatable

Associated Types

type Output = Bool

Methods

public func equal(to: Self) -> Bool

Bridges Equal.equal(to:) to Equatable.isEqual(to:).

public func isEqual(to: Result[T, E]) -> Bool

Structural equality on the result. Backs ==.

Examples

Ok(1) == Ok(1); // true Ok(1) == Ok(2); // false Err("x") == Err("x"); // true Ok(1) == Err("x"); // false
public func notEqual(to: Self) -> Bool

Default !=: delegates to == so there's a single source of truth.

ImplementsFormattable

Methods

public func format(into: mutating StringBuilder, FormatOptions)

Renders Ok(...) or Err(...), forwarding options to the inner format for the payload.

public func formatted(FormatOptions) -> String

Returns this value rendered as a String.

Convenience wrapper: creates a StringBuilder, calls format(into:), and returns the built string. Uses a distinct name to avoid overload-resolution ambiguity with format(into:).

Defined in lang/std/result/result.ks