Copyright | Ivan Lazar Miljenovic |
---|---|
License | MIT |
Maintainer | Ivan.Miljenovic@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
- withFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r
- withBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r
- writeBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteString m r -> m r
- appendBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteString m r -> m r
- withBinaryFileContents :: (MonadMask m, MonadIO m, MonadIO n) => FilePath -> (ByteString n () -> m r) -> m r
- class MonadCatch m => MonadMask m
- bracket :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c
File-handling
withFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r Source #
A lifted variant of withFile
.
You almost definitely don't want to use this; instead, use
withBinaryFile
in conjunction with Data.ByteString.Streaming.
withBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r Source #
A lifted variant of withBinaryFile
.
Common file-handling cases
writeBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteString m r -> m r Source #
Write to the specified file.
appendBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteString m r -> m r Source #
Append to the specified file.
withBinaryFileContents :: (MonadMask m, MonadIO m, MonadIO n) => FilePath -> (ByteString n () -> m r) -> m r Source #
Apply a function to the contents of the file.
Note that a different monadic stack is allowed for the
ByteString
input, as long as it later gets resolved to the
required output type (e.g. remove transformer).
Re-exports
These may assist in writing your own bracket-style functions.
Note that not everything is re-exported: for example, Handle
isn't
re-exported for use with withFile
as it's unlikely that you will
write another wrapper around it, and furthermore it wouldn't be a
common enough extension to warrant it.
class MonadCatch m => MonadMask m #
A class for monads which provide for the ability to account for all
possible exit points from a computation, and to mask asynchronous
exceptions. Continuation-based monads, and stacks such as ErrorT e IO
which provide for multiple failure modes, are invalid instances of this
class.
Note that this package does provide a MonadMask
instance for CatchT
.
This instance is only valid if the base monad provides no ability to
provide multiple exit. For example, IO
or Either
would be invalid base
monads, but Reader
or State
would be acceptable.
Instances should ensure that, in the following code:
f `finally` g
The action g
is called regardless of what occurs within f
, including
async exceptions.
MonadMask IO | |
(~) * e SomeException => MonadMask (Either e) | Since: 0.8.3 |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
MonadMask m => MonadMask (StateT s m) | |
MonadMask m => MonadMask (IdentityT * m) | |
MonadMask m => MonadMask (StateT s m) | |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
MonadMask m => MonadMask (ReaderT * r m) | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
bracket :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c #
Generalized abstracted pattern of safe resource acquisition and release
in the face of exceptions. The first action "acquires" some value, which
is "released" by the second action at the end. The third action "uses"
the value and its result is the result of the bracket
.
If an exception occurs during the use, the release still happens before the exception is rethrown.