mtl icon indicating copy to clipboard operation
mtl copied to clipboard

How to combine two different `r` `MonadReaders` ?

Open diqye opened this issue 10 months ago • 1 comments

I wrote some code as below


testa :: (MonadReader Int m, MonadReader Char m, MonadIO m) => m ()
testa = do
    i <- ask
    c <- ask
    liftIO $ do
        print (i :: Int)
        print (c :: Char)

runTesta :: IO ()        
runTesta = flip runReaderT 100 $  flip runReaderT 'H' testa

When I run, it will be appear some errors image

diqye avatar Apr 28 '24 07:04 diqye

Due to the functional dependency on MonadReader, you can't have any m with two different environments like that. You'd have to do something like a MonadReader (Char, Int) m constraint, then ask would get you both.

kozross avatar Apr 28 '24 16:04 kozross