APIRuntime.Scope
Runtime.Scope
Cleanup scopes: releasing what a computation acquired, however it exits.
A release runs exactly once when its scope exits: on a normal return, on
an abort aimed at any outer handler, and on a failure inside a handler’s
return clause. Nested scopes release in reverse order of acquisition.
bracket
bracket : (() ->{e} a) -> (a ->{e} ()) -> (a ->{e} b) ->{e} bSource
Acquires a resource with acquire, passes it to use, and releases it
with release when the scope exits. If acquisition fails there is nothing
to release. If use fails, that failure stays primary even when the
release fails too; if only the release fails, its failure propagates.
import Fail
import Result exposing (Result(..))
import Runtime.Scope
import State
released = State.run [] { Runtime.Scope.bracket { 5 } { _ -> State.put ["released"] } { r -> r * 2 } }
failed =
State.run [] {
Fail.attempt { Runtime.Scope.bracket { 5 } { _ -> State.put ["released"] } { r -> if r > 0 then Fail.fail "boom" else r } }
}
released.value == 10
released.state == ["released"]
failed.value == Err "boom"
failed.state == ["released"]
finally
finally : (() ->{e} a) -> (() ->{e} ()) ->{e} aSource
Runs action, then cleanup when it exits, however it exits.
import Runtime.Scope
import State
log = State.run [] { Runtime.Scope.finally { State.put ["body"] } { State.put ["cleanup" | State.get ()] } }
log.state == ["cleanup", "body"]
