Silk

silk/result

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 as Result

fn half(value: i32) -> Result.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)
  let failed = Result.failResult<i32, i32>(7)
  if Result.isFailure<i32, i32>(&failed) {} else {
    return 0
  }
  return Result.unwrapOr<i32, i32>(move answer, 0)
}

Import as Result with import silk.result.

Public declarations: 11.

Success

pub struct Success<A>

The successful member of a completed Result.

Failure

pub struct Failure<F>

The failed member of a completed Result.

Result

pub struct 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; borrow it for isSuccess and isFailure when the payload must remain available.

succeed

pub fn succeed<A, F>(value: A) -> silk/result.Result<A, F>

Constructs a completed success by moving value into the success arm.

failResult

pub fn failResult<A, F>(error: F) -> silk/result.Result<A, F>

Constructs a completed failure by moving error into the failure arm.

map

pub fn map<A, B, F>(self: silk/result.Result<A, F>, transform: once fn(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.

mapError

pub fn mapError<A, F, G>(self: silk/result.Result<A, F>, transform: once fn(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.

flatMap

pub fn flatMap<A, B, F>(self: silk/result.Result<A, F>, transform: once fn(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.

unwrapOr

pub fn unwrapOr<A, F>(self: silk/result.Result<A, F>, fallback: A) -> A

Returns 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: A

The owned alternative consumed only when self is a failure.

isSuccess

pub fn isSuccess<A, F>(self: &silk/result.Result<A, F>) -> bool

Returns true when the borrowed outcome is Success, without consuming either payload.

isFailure

pub fn isFailure<A, F>(self: &silk/result.Result<A, F>) -> bool

Returns true when the borrowed outcome is Failure, without consuming either payload.

On this page