-
I have a function with a pure value that can log messages: {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
import Colog (Message, WithLog, logInfo, logMessagePure, runPureLog, usingLoggerT)
zeroM :: WithLog r Message m => m Int
zeroM = 0 <$ logInfo "Hello world!" I'd like to remove its monadic context by discarding logs. I found an example that shows how to do this: co-log/co-log/tutorials/Main.hs Line 204 in 72fbe39 Here is my attempt on discarding the log messages of zero :: Int
zero = fst . runPureLog $ usingLoggerT logMessagePure zeroM However, the types does not line up and my compiler complains. How do I discard the logs, keeping only returned value? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @qwbarch 👋🏻 The logMessagePure :: Monad m => LogAction (PureLoggerT msg m) msg Because of how type inference works, Haskell is not able to infer that zero :: Int
zero = fst . runPureLog $ usingLoggerT (logMessagePure :: LogAction (PureLogger Message) Message) zeroM Alternatively, you can enable the
Moreover, there's even a better way to ignore logging. zero :: Int
zero = runIdentity $ usingLoggerT (mempty :: LogAction Identity Message) zeroM |
Beta Was this translation helpful? Give feedback.
Hi @qwbarch 👋🏻
The
logMessagePure
function is polymorphic over the message type:Because of how type inference works, Haskell is not able to infer that
msg
is the same asMessage
in your example. A simple solution would be to give Haskell hint by specifying the type oflogMessagePure
explicitly:Alternatively, you can enable the
TypeApplications
extension and specify this type a bit shorter:Moreover, there's even a better way to ignore lo…