-
Notifications
You must be signed in to change notification settings - Fork 35
Closed
Description
Assume that I have the following Effect:
data Severity = Info | Error | Fatal
deriving (Show, Eq, Ord, Bounded)
data Logger e :: Effect where
Log :: Severity -> String -> (Logger e) m ()
type instance DispatchOf (Logger e) = Dynamic
log :: (Logger e :> es) => Severity -> String -> Eff es ()
log severity message = send $ Log severity messageI would like to write an interpreter that:
- Takes a
FilePathas argument - Opens the file in
WriteModeonce - Every 10 writes it flushes the file handle
(this is just an example use case, not something real)
As a skeleton I have something like:
runFileLogger :: (IOE :> es) => FilePath -> Eff (Logger e : es) a -> Eff es a
runFileLogger path = do
-- Use `withFile` or similar to get a `Handle`. The file should be opened once.
-- withFile path WriteMode $ \handle ->
-- Store some kind of counter variable (`IORef` maybe?)
-- counter <- ???
interpret $ \env op -> case op of
Log severity message -> do
-- update the counter
liftIO $ hPutStrLn handle message
-- if the counter reaches 10, call `hFlush handle`- The signature
runFileLogger :: (IOE :> es) => FilePath -> Eff (Logger e : es) a -> Eff es ais what I would like to have. - Notice how certain operations should be executed "before" the
interpretcall - We're dealing with the
withXXXpattern since we want to do something before & after interpreting the Effect: open the file and closing it.
Metadata
Metadata
Assignees
Labels
No labels