Typed failures.
A typed failure is a value that an effect raises with fail instead of succeeding. The failure row ! E of an effect function or Effect type lists the failure types it can raise; callers must declare or handle each of them.
Syntax#
FailureRow = '!' FailureType ( '|' FailureType )*
FailureType = Type | 'Without' '<' Row ',' Row '>'
Row = Type ( '|' Type )*
FailStatement = 'fail' ExpressionA failure row follows the result type of an
effect fn, an anonymouseffect fn, or the success type insideEffect<...>. See Effects and run.
Failure rows#
! Edeclares that the effect may fail with a value of typeE.! A | Bdeclares several failure types.Without a failure row, an effect cannot fail.
! neversays the same explicitly.Any value type can be a failure type: structs, unions, enums, primitives such as
i32,string<'static>, and references.Without<A | B, A>is the rowA | BwithAremoved. It appears in the result ofEffect.catch.Only effect functions may declare a failure row. On an ordinary
fnit is SEM0062.A failure type in the signature of a
pubfunction must itself be public (SEM0019).A generic parameter can stand for a whole failure row:
effect fn f<E>(e: Effect<'static; i32 ! E>) -> i32 ! E.
fail#
fail eends the current effect with failuree. It is a statement and never completes normally.failis valid only in aneffect fn, an anonymouseffect fnor aneffect { ... }block. Elsewhere it is SEM0063.The type of
emust be a member of the declared failure row, or a union of members. Otherwise it is SEM0064.In an effect without a failure row, or with
! never, everyfailis SEM0064.failmoves its operand. Failing with a non-Copy local needsfail move x(OWN0003).An effect block has no declared row: its row is the union of what it can fail with.
Propagation through run#
run einside aneffect fnpropagates every failure ofeto the caller unchanged.Each failure type of
emust appear in the enclosing function's row. A missing one is SEM0066.A wider row is accepted: an effect failing with
Amay run inside a function declared! A | B.In an ordinary
fn,runis valid only when every failure has been handled (SEM0066).A failure that escapes
pub effect fn mainends the program with status 1 and a report on standard error. See Program entry and termination.Effect.flatMap,Effect.zipand the other sequencing combinators stop at the first failure and skip the later steps.
Recovering#
The recovery combinators belong to the struct
Effectinsilk/core/effect. Each returns a new lazy Effect.
| Member | Result | Behavior |
|---|---|---|
Effect.catchAll(e, h) | A | B ! F | Runs h(error) on any failure of e; only h's own failures F remain |
Effect.catch<S>(e, h) | A | B ! Without<E, S> | F | Handles only member S; other members propagate unchanged |
Effect.mapError(e, f) | A ! F | Replaces the failure with plain f(error) |
Effect.mapBoth(e, onSuccess, onFailure) | B ! F | Maps the success or the failure with plain callbacks |
Effect.result(e) | Result<A, E> | Never fails; the outcome becomes data |
Effect.retry(e, n) | A ! E | Runs e again after a failure, at most n more times |
A handler
his a callable that returns an Effect, usually aneffect fntaking the failure value. It receives the failure by value.A handler runs only on failure. On success the handler is never called and is dropped.
The handler's success type joins the protected success type: a handler returning
boolfor ani32effect givesi32 | bool.A handler declared
-> never ! Fre-raises a different failure. The success type staysA.For
catchAll, the handler's parameter type is the whole failure row, for exampleA | B. Match on it to tell members apart. See Patterns and match.The parameter may also be a wider closed union than the row:
effect fn either(failure: Missing | Denied) -> i32handles an Effect that fails only withMissing. The failure is widened to the parameter's union at the call.A handler may be a partially applied effect function. The given arguments fill the trailing parameters and the failure fills the first: with
effect fn recover(error: Missing, base: i32) -> i32,Effect.catchAll(recover(5))runsrecover(error, 5).For
catch,Smay be written explicitly or inferred from the handler's parameter.Smust be a member of the row (where S in E). Otherwise it is SEM0067: "Invalid Effect.catch handler:main.Otheris not a failure of the handled Effect, which fails withmain.Problem".A handler is an effect function. A plain
fnhandler is SEM0067, and the message says to declare iteffect fnwith the same parameter and result.For
catchAll, a handler whose parameter does not accept every failure of the row is SEM0067 at the handler. Withexplain(failure: Oops)for a rowOops | LogError: "Invalid Effect.catch handler: its parameter typemain.Oopsdoes not acceptsilk.io.logger.LogError, a failure of the handled Effect; add it to the parameter's union". The message is the same for a union parameter that lacks a member, for an Effect with a single failure, and in the pipeline forme |> Effect.catchAll(h). With several missing failures it lists them all and says "add them to the parameter's union".Requirements of the protected Effect and of the handler both remain. See Services and providers.
retrytakes a reusablemut Effect.retry(e, 0)runs once. Earlier failure values are dropped before the next attempt; the last failure propagates.
Result#
Result<A, F>fromsilk/core/resultis a union with membersSuccess { value: A }andFailure { error: F }.It is completed data: it does not run, needs no providers and does not propagate.
Members:
Result<A, F>.succeed(v),Result<A, F>.failResult(e),map,mapError,flatMapandunwrapOr.Effect.result(e)runseand returnsResult<A, E>. For a rowA | B, the error type is the unionA | B.
Finalizers#
Effect.ensuring(e, finalizer)runsfinalizeraftere, whetheresucceeds or fails, then passes one's original outcome.The finalizer has type
once Effect<'env; () ! never ? S>: it cannot fail. Passing a fallible finalizer is rejected (SEM0052). Recover its failures first, for example withEffect.catchAll.The finalizer's requirements
Sjoin the result's requirements.Order on failure: the failing frame drops its locals, then the finalizer runs, then the failure reaches the handler.
Nested
ensuringfinalizers run innermost first.Effect.ensuringNonParkingandEffect.useReleaseNonParkingalso run when a parked execution is cancelled. Their finalizers must not suspend. See Suspension and cancellation.
Drops on failure#
When an effect exits through
fail, or through a failingrun, its live locals are dropped before the failure leaves the frame. Drop hooks run. See Ownership and moves.Each frame the failure passes through drops its own locals in the same way.
Values already evaluated for a call whose
runthen fails are dropped too.A recovered failure value belongs to the handler, or to the
Result. It is dropped there like any other value.
never#
neveris the type of a computation that does not complete. It has no values.-> never ! Edeclares an effect that can only fail.runof it has typenever, soreturn run stop(code)fits any result type.! neveris an empty failure row. Finalizers require it.A
runwhose success type isnevermay stand alone as a statement.
Failures and traps#
A trap is a fatal runtime error such as arithmetic overflow, division by zero or an out-of-bounds index. See Program entry and termination.
Traps are not typed failures. They bypass
catch,catchAll,result,retry,ensuringfinalizers and Drop hooks. The process stops at once.Use the checked arithmetic functions of the integer modules, such as
i32.checkedAdd, to turn a would-be trap into a value the program can fail with.
Examples#
Selective and complete recovery of a two-member row:
import silk.core.effect { Effect }
struct Missing {
code: i32,
}
struct Denied {
code: i32,
}
effect fn open(mode: i32) -> i32
! Missing | Denied {
if mode == 1 {
fail Missing { code: 10 }
}
if mode == 2 {
fail Denied { code: 20 }
}
return 0
}
effect fn useDefault(missing: Missing) -> i32 {
return missing.code
}
effect fn partial(mode: i32) -> i32
! Denied {
return run open(mode) |> Effect.catch(useDefault)
}
effect fn either(error: Missing | Denied) -> i32 {
return match move error {
Missing { code } => code
Denied { code } => code + 1
}
}
effect fn last(denied: Denied) -> i32 {
return denied.code
}
pub fn main() -> i32 {
let a = run partial(1) |> Effect.catchAll(last)
let b = run partial(2) |> Effect.catchAll(last)
let c = run open(2) |> Effect.catchAll(either)
return a + b + c - 51
}Mapping a failure and inspecting the outcome as data:
import silk.core.effect { Effect }
import silk.core.result { Result }
struct Missing {
code: i32,
}
struct Wrapped {
inner: i32,
}
effect fn load(ok: bool) -> i32
! Missing {
if !ok {
fail Missing { code: 4 }
}
return 10
}
fn wrap(missing: Missing) -> Wrapped {
return Wrapped { inner: missing.code }
}
effect fn fallback(error: Wrapped) -> bool {
return false
}
pub fn main() -> i32 {
let outcome = run Effect.result(Effect.mapError(load(false), wrap))
let code = match move outcome {
Result<i32, Wrapped>.Success { value } => value
Result<i32, Wrapped>.Failure { error } => error.inner
}
if code != 4 {
return 1
}
let mixed: i32 | bool = run Effect.catchAll(Effect.mapError(load(false), wrap), fallback)
let n = match mixed {
i32 v => v
bool _ => 7
}
return n - 7
}A failure drops the frame's locals, runs the finalizers innermost first, then reaches the handler:
import silk.core.effect { Effect }
import silk.core.ownership { Drop }
struct Problem {
code: i32,
}
struct Guard<'a> {
count: &'a mut i32,
}
impl<'a> Drop for Guard<'a> {
fn drop(self: &mut Guard<'a>) -> () {
self.count.* = self.count.* + 1
return ()
}
}
service Journal {
effect fn mark(value: i32) -> () ? &mut Journal
}
struct Memory {
value: i32,
}
effect fn mark(self: &mut Memory, value: i32) -> () {
self.value = self.value * 10 + value
return ()
}
impl Journal for Memory {
mark: Memory.mark
}
effect fn work(count: &mut i32) -> i32
! Problem
? &mut Journal {
let _guard = Guard { count: move count }
run Journal.mark(1)
fail Problem { code: 3 }
}
effect fn note(value: i32) -> ()
? &mut Journal {
return run Journal.mark(value)
}
effect fn recover(error: Problem) -> i32
? &mut Journal {
run Journal.mark(4)
return error.code
}
pub fn main() -> i32 {
let mut drops = 0
let mut journal = Memory { value: 0 }
let inner = Effect.ensuring(work(&mut drops), note(2))
let outer = Effect.ensuring(move inner, note(3))
let code = run Effect.catchAll(move outer, recover) |> Effect.provideMut(&mut journal)
if code != 3 || drops != 1 {
return 1
}
if journal.value != 1234 {
return 2
}
return 0
}Diagnostics#
| Code | Meaning |
|---|---|
| SEM0019 | A pub signature names a private failure type |
| SEM0052 | Effect.ensuring given a finalizer that can fail (catalog: type arguments cannot be inferred) |
| SEM0062 | Failure row on an ordinary function |
| SEM0063 | fail outside an effect function or effect block |
| SEM0064 | fail with a type the effect's row does not declare |
| SEM0066 | run leaves failures the enclosing signature does not declare |
| SEM0067 | Effect.catch or Effect.catchAll handler does not fit the failures it handles |
| OWN0003 | fail with a non-Copy local needs move |
See also#
Effects and run · Services and providers · Program entry and termination · Ownership and moves · Patterns and match · Enums and unions · Suspension and cancellation · silk/core/effect, silk/core/result
Open questions#
SEM0061 ("failure type must be a detached ordinary value type") has no producer in the compiler. A reference failure type such as
! &i32is accepted, andEffect.catchAllcan recover it.