我试图在ask
约束的do
表示法块中调用MonadReader
,但遇到有关刚性类型的错误。据我所知,ask
是为所有MonadReader
定义的?
bar :: MonadReader r m => m r
bar = do
i <- ask
return i
这将返回
• Couldn't match type ‘m’ with ‘ReaderT r m0’ ‘m’ is a rigid type variable bound by the type signature for: bar :: forall r (m :: * -> *). MonadReader r m => m r at mem.hs:50:8 Expected type: m r Actual type: ReaderT r m0 r • In a stmt of a 'do' block: i <- ask In the expression: do { i <- ask; return i } In an equation for ‘bar’: bar = do { i <- ask; return i }
答案 0 :(得分:6)
似乎您已经从onCreateView()
导入了ask
,但是却从Control.Monad.Trans.Reader
导入了MonadReader
。 Control.Monad.Reader
中的ask
函数是单态的,并且仅与Control.Monad.Trans.Reader
一起使用。只需自行导入ReaderT
,即可重新导出Control.Monad.Reader
,但它提供了ReaderT
,ask
和local
的通用版本。