Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Support for access to a write only value of a particular type.
The value is thread local. If you want it to be shared between threads, use Effectful.Writer.Static.Shared.
Warning: Writer
's state will be accumulated via left-associated uses
of <>
, which makes it unsuitable for use with types for which such pattern
is inefficient. This applies, in particular, to the standard list type,
which makes the Writer
effect pretty niche.
Note: while the Writer
from the
transformers
package includes additional operations
pass
and
censor
, they don't cooperate with runtime
exceptions very well, so they're deliberately omitted here.
Synopsis
- data Writer w :: Effect
- runWriter :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
- execWriter :: Monoid w => Eff (Writer w : es) a -> Eff es w
- tell :: (Writer w :> es, Monoid w) => w -> Eff es ()
- listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
- listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b)
Effect
data Writer w :: Effect Source #
Provide access to a strict (WHNF), thread local, write only value of type
w
.
Instances
type DispatchOf (Writer w) Source # | |
Defined in Effectful.Writer.Static.Local | |
newtype StaticRep (Writer w) Source # | |
Defined in Effectful.Writer.Static.Local |
Handlers
runWriter :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w) Source #
Run a Writer
effect and return the final value along with the final
output.
execWriter :: Monoid w => Eff (Writer w : es) a -> Eff es w Source #
Run a Writer
effect and return the final output, discarding the final
value.
Operations
tell :: (Writer w :> es, Monoid w) => w -> Eff es () Source #
Append the given output to the overall output of the Writer
.
listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w) Source #
Execute an action and append its output to the overall output of the
Writer
.
Note: if an exception is received while the action is executed, the partial
output of the action will still be appended to the overall output of the
Writer
:
>>>
:{
runEff . execWriter @String $ do tell "Hi" handle (\(_::ErrorCall) -> pure ((), "")) $ do tell " there" listen $ do tell "!" error "oops" :} "Hi there!"