haskell / mtl

The Monad Transformer Library
http://www.haskell.org/haskellwiki/Monad_Transformers
Other
360 stars 63 forks source link

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

Open diqye opened 4 months ago

diqye commented 4 months ago

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

kozross commented 4 months ago

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.