silk/result
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.
Completed success-or-failure values that can be inspected and transformed as ordinary data.
When to use
Use Result after an effectful computation has been reified, or whenever both outcome arms
belong in a value. Use map for success, mapError for failure, and flatMap for a
success continuation that already returns a result.
Details
Result<A, F> owns either Success or Failure. Its combinators move the selected payload
forward and preserve the other arm without inventing a runtime failure-row descriptor.
Unlike an Effect<A ! F>, a Result<A, F> is already completed ordinary data: it does not run,
require a provider, or propagate through fail. Use Effect.result to turn one Effect execution
into a Result when a caller needs to inspect or store the outcome.
Examples
Transform a success and choose a fallback for failure
import silk.result { Result }
fn half(value: i32) -> Result<i32, i32> {
if value == 0 {
return Result.failResult<i32, i32>(2)
}
return Result.succeed<i32, i32>(value / 2)
}
fn addTwo(value: i32) -> i32 {
return value + 2
}
pub fn main() -> i32 {
let initial = Result.succeed<i32, i32>(80)
let halved = Result.flatMap<i32, i32, i32>(move initial, half)
let answer = Result.map<i32, i32, i32>(move halved, addTwo)
return Result.unwrapOr<i32, i32>(move answer, 0)
}Import as Result with import silk.result { Result }.
Public declarations: 1.
Result
pub union Result<A, F>One completed outcome: either a success carrying A or a failure carrying F.
Details
Result is the reified form of an Effect that has already run. Reifying an Effect turns its
failure row into ordinary value data, which is what lets the failure combinators in
silk.effect be written as ordinary Silk source instead of compiler built-ins.
A Result is consumed when matched or passed to a transforming combinator. Use a borrowed
match when the payload must remain available.
Success
Result<A, F>.Success { value: A }: Result<A, F>A completed success.
Field value
pub value: AThe produced success value.
Failure
Result<A, F>.Failure { error: F }: Result<A, F>A completed failure.
Field error
pub error: FThe produced failure value.
Associated function Result.succeed
pub fn succeed<A, F>(value: A) -> silk/result.Result<A, F>Constructs a completed success by moving value into the success arm.
Associated function Result.failResult
pub fn failResult<A, F>(error: F) -> silk/result.Result<A, F>Constructs a completed failure by moving error into the failure arm.
Method Result.map
pub fn map<A, F, B, 'life3>(self: Result<A, F>, transform: once fn<'life3>(A) -> B) -> silk/result.Result<B, F>Applies transform once to a success value and carries a failure through unchanged.
Details
The callback is never called for Failure. This consumes the result and may change only its
success type; use mapError to change the failure type instead.
Method Result.mapError
pub fn mapError<A, F, G, 'life3>(self: Result<A, F>, transform: once fn<'life3>(F) -> G) -> silk/result.Result<A, G>Applies transform once to a failure value and carries a success through unchanged.
Details
The callback is never called for Success. This consumes the result and may change only its
failure type.
Method Result.flatMap
pub fn flatMap<A, F, B, 'life3>(self: Result<A, F>, transform: once fn<'life3>(A) -> silk/result.Result<B, F>) -> silk/result.Result<B, F>Continues a success with a transform that answers with a Result of its own, so the outcome stays one Result deep instead of nesting.
Details
A failure bypasses the callback unchanged. The callback must use the same failure type F, so
use mapError before or after this operation when the steps use different error types.
Method Result.unwrapOr
pub fn unwrapOr<A, F>(self: Result<A, F>, fallback: A) -> AReturns the success value, or the fallback value when the outcome is a failure.
Details
Only the failure arm consumes the fallback. The success arm releases it, and the failure arm
releases the error, so exactly one owned value leaves this call and the other drops.
Use match instead when the failure payload affects recovery or must be retained.
Parameter fallback
fallback: AThe owned alternative consumed only when self is a failure.