我想读取带有自定义提示的输入字符串,但是提示字符串来自不纯的上下文,因此我不能readInputLine
按原样使用。我试图根据这个答案实现一个功能
getLineIO :: MonadException m => IO String -> InputT m (Maybe String)
getLineIO ios = do
s <- ios
res <- getInputLine s
lift res
但我得到一个错误
Couldn't match expected type ‘InputT m String’
with actual type ‘IO String’
Relevant bindings include
getLineIO :: IO String -> InputT m (Maybe String)
(bound at Main.hs:38:1)
In a stmt of a 'do' block: s <- ios
In the expression:
do { s <- ios;
return $ getInputLine s }
更新:根据@bheklilr 的回答让它工作
getLineIO :: (MonadException m, MonadIO m) => IO String -> InputT m (Maybe String)
getLineIO ios = do
s <- liftIO ios
getInputLine s