tur/typeclass-monaderror
minimal MonadError typeclass stub.
Auto-loaded (and loadable directly) so a module can declare (definstance MonadError [...] ...) without importing the full typeclass.tur. The full typeclass.tur remains available on demand via (load "stdlib/typeclass.tur"); defclass redefinition is idempotent, so the two coexist.
Since: stdlib-hkt-consolidation T4
MonadError
(defclass MonadError [^m] [(Monad m)])
a Monad with an error channel you can raise into and recover from.
| ^m | the computation's type constructor (kind * -> *), e.g. (Result _ B) |
(throw-error [err] :int)-
a failed computation carrying
err-- an err for Result. (catch-error [ma handler :fn] :int)-
maunchanged if it succeeded; otherwise the computationhandlerbuilds from the error.handleris a:fncarrier, so a capturing closure works.
(catch-error (throw-error e) h) = (h e) (catch-error (pure x) h) = (pure x)
(ok-val (:: (catch-error (:: (err 5) (Result int int)) (fn [e : int] : (Result int int) (ok (* e -1)))) (Result int int))) ; => -5
Both methods return the :int carrier, so ascribe the result to read it: (:: (catch-error ma h) (Result int int)).
Superclass: Monad.
Since: stdlib-hkt-consolidation T4