feat: add finally

This commit is contained in:
Leonardo de Moura 2019-11-09 07:28:12 -08:00
parent f063bf4a40
commit d10e08236f

View file

@ -184,3 +184,9 @@ instance (ε m out) [MonadRun out m] : MonadRun (fun α => out (Except ε α)) (
-- useful for implicit failures in do-notation
instance (m : Type → Type) [Monad m] : MonadFail (ExceptT String m) :=
⟨fun _ => throw⟩
/-- Execute `x` and then execute `finalizer` even if `x` threw an exception -/
@[inline] def finally {ε α β : Type u} {m : Type u → Type v} [Monad m] [MonadExcept ε m] (x : m α) (finalizer : m β) : m α :=
catch
(do a ← x; finalizer; pure a)
(fun e => do finalizer; throw e)