Copyright | Copyright 2022 Shea Levy. |
---|---|
License | Apache-2.0 |
Maintainer | shea@shealevy.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- class Monad m => MonadCleanup m where
- generalCleanup :: m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c)
- data AbortException = AbortException
- withCleanup :: MonadCleanup m => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c
- newtype CleanupFromMask m a = CleanupFromMask (m a)
- newtype CleanupNoException m a = CleanupNoException (m a)
Documentation
class Monad m => MonadCleanup m where Source #
Monads that can cleanup within a single monadic scope.
MonadCleanup
s allow for acquiring some resource and
guaranteeing that it will be released,
if computation might continue within that monad.
This is very similar to MonadMask
, with the following differences:
MonadCleanup
s may not be able to throw or catch exceptions (noMonadCatch
superclass) or mask exceptions.- The guarantee of
generalCleanup
is not as absolute asgeneralBracket
(though the latter always has theSIGKILL
/power goes out exception). If we can't handle exceptions at all in the monad (at least not withoutunsafePerformIO
), then sometimes the cleanup function won't be called, but only in cases where the entire computation the monad is running is going to be aborted.
This allows MonadCleanup
to be used in pure contexts (see CleanupNoException
) and still
provide meaningful semantics.
:: m a | Acquire some resource |
-> (a -> ExitCase b -> m c) | Release the resource, observing the outcome of the inner action |
-> (a -> m b) | Inner action to perform with the resource |
-> m (b, c) |
Acquire some resource, use it, and clean it up.
cleanup is guaranteed to run if computation in the surrounding monadic scope might continue.
Similar to generalBracket
, see documentation of MonadCleanup
for the differences.
Instances
data AbortException Source #
An Exception
corresponding to the ExitCaseAbort
exit case.
Instances
Exception AbortException Source # | |
Defined in Control.Monad.Cleanup | |
Show AbortException Source # | |
Defined in Control.Monad.Cleanup showsPrec :: Int -> AbortException -> ShowS # show :: AbortException -> String # showList :: [AbortException] -> ShowS # |
withCleanup :: MonadCleanup m => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c Source #
Acquire some resource, use it, and clean it up.
This is to bracketWithError
as generalCleanup
is to generalBracket
, see documentation
of generalCleanup
for more details.
newtype CleanupFromMask m a Source #
A DerivingVia helper for deriving MonadCleanup
from MonadMask
.
CleanupFromMask (m a) |
Instances
newtype CleanupNoException m a Source #
A DerivingVia for deriving MonadCleanup
in a Monad
which
can't handle exceptions.
If cleanup runs at all, it will run in ExitCaseSuccess
Note that the associated MonadCleanup
instance is invalid if it is possible to catch exceptions in the monad!
CleanupNoException (m a) |