Result¶
Result type inspired by Rust's Result enum.
A disciplined alternative to raising exceptions — the Result type makes success
and failure explicit in your function signatures. Conceptually it is the same
idea as :class:Either in Scala or Haskell, specialised so that the right
side holds a success value and the left side holds an error.
Use :class:Ok to wrap a success value and :class:Err to wrap an error, then
compose operations with :meth:map, :meth:flat_map, and :meth:map_error
instead of writing try/except blocks.
result
¶
Result type inspired by Rust's Result enum.
A disciplined alternative to raising exceptions — the Result type makes success
and failure explicit in your function signatures. Conceptually it is the same
idea as :class:Either in Scala or Haskell, specialised so that the right
side holds a success value and the left side holds an error.
Use :class:Ok to wrap a success value and :class:Err to wrap an error, then
compose operations with :meth:map, :meth:flat_map, and :meth:map_error
instead of writing try/except blocks.
Result
¶
Bases: Protocol
Protocol that defines the shared interface for :class:Ok and :class:Err.
Source code in src/forging_blocks/foundation/result/result.py
is_ok: bool
property
¶
Return True when this Result holds a success value (is :class:Ok).
is_err: bool
property
¶
Return True when this Result holds an error (is :class:Err).
value: ValueType
property
¶
Unwrap and return the success value.
Raises :class:ResultAccessError if this Result is an :class:Err.
error: ErrorType
property
¶
Unwrap and return the error.
Raises :class:ResultAccessError if this Result is an :class:Ok.
map(fn: Callable[[ValueType], MappedValueType]) -> Result[MappedValueType, ErrorType]
¶
Transform the success value with fn, passing errors through unchanged.
This is the Functor map — fmap in Haskell, .map on Scala's
Option / Either.
Source code in src/forging_blocks/foundation/result/result.py
map_error(fn: Callable[[ErrorType], MappedErrorType]) -> Result[ValueType, MappedErrorType]
¶
Transform the error with fn, passing success values through unchanged.
flat_map(fn: Callable[[ValueType], Result[MappedValueType, ErrorType]]) -> Result[MappedValueType, ErrorType]
¶
Chain a fallible operation that itself returns a Result.
If Ok, call fn with the value; if Err, short-circuit unchanged.
This is Monad's bind — >>= in Haskell, flatMap in Scala.
Source code in src/forging_blocks/foundation/result/result.py
get_value_or(default: ValueType) -> ValueType
¶
Return the wrapped success value if Ok, otherwise return the provided default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
ValueType
|
The value to return if this result is an error. |
required |
Returns:
| Type | Description |
|---|---|
ValueType
|
The unwrapped success value if Ok; otherwise, |
Source code in src/forging_blocks/foundation/result/result.py
get_value_or_else(fn: Callable[[ErrorType], ValueType]) -> ValueType
¶
Return the wrapped success value if Ok, otherwise compute one from the error.
Applies fn to the wrapped error (if present) to compute and return a recovery value.
Useful for transforming or logging errors before fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[ErrorType], ValueType]
|
A callable that accepts the error and returns a recovery value of the same type |
required |
Returns:
| Type | Description |
|---|---|
ValueType
|
The unwrapped success value if Ok; otherwise, the result of |