Allocate resources which are guaranteed to be released.
One point to note: all register cleanup actions live in IO, not the main monad. This allows both more efficient code, and for monads to be transformed.
- data ResourceT m a
- runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a
- mapResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n b
- class MonadIO m => MonadResource m where
- data ReleaseKey
ResourceT monad transformer
The Resource transformer. This transformer keeps track of all registered
actions, and calls them upon exit (via
runResourceT). Actions may be
register, or resources may be allocated atomically via
with function corresponds closely to
functions are provided by 'ResourceT'\'s
Releasing may be performed before exit via the
release function. This is
a highly recommended optimization, as it will ensure that scarce resources
are freed early. Note that calling
release will deregister the action, so
that a release action will only ever be called once.
Pass-through instances for the
mtl type classes are provided
automatically by the
|MonadBaseControl b m => MonadBaseControl b (ResourceT m)|
|Monad m => Monad (ResourceT m)|
|Monad m => Functor (ResourceT m)|
|MonadPlus m => MonadPlus (ResourceT m)|
|Monad m => Applicative (ResourceT m)|
|MonadPlus m => Alternative (ResourceT m)|
|(MonadFork m, MonadBaseControl IO m) => MonadFork (ResourceT m)|
|MonadBaseControl IO m => MonadResource (ResourceT m)|
ResourceT transformer, and call all registered release
Note that there is some reference counting involved due to the
fork used in the
MonadFork instance. If multiple
threads are sharing the same collection of resources, only the last call
runResourceT will deallocate the resources.
Transform the monad a
ResourceT lives in. This is most often used to
strip or add new transformers to a stack, e.g. to run a
MonadResource type class
The others instances are overlapping instances (in the spirit of
mtl-evil-instances), which provide automatic pass-through instances for
MonadResource for every monad transformer. This means that you don't have
to provide a pass-through instance of
MonadResource for every monad
transformer you write.
Perform some allocation, and automatically register a cleanup action.
Call a release action early, and deregister it from the list of cleanup actions to be performed.