【发布时间】:2021-02-04 10:00:39
【问题描述】:
社区您好,感谢您抽出宝贵时间。
我有一个错误,我不确定错误是什么,但我认为问题是:
ext-1.2.4.1:Data.Text.Internal.Lazy.Text IO)到Web.Scotty.Internal.Types.ScottyT之间没有IO转换器。
但我想知道为什么编译器可以使用ext-1.2.4.1:Data.Text.Internal.Lazy.Text IO)。这就是为什么我只使用 String 并删除了所有出现的 {-# LANGUAGE OverloadedStrings #-} 但仍然出现错误的原因。另一方面,这应该是IO [String],不是吗?
正如你可以提到的,我真的不知道ext-1.2.4.1:Data.Text.Internal.Lazy.Text IO) 是什么。
在另一个地方,我已经成功地将liftIO 用于a -> IO String 函数。而且我认为我使用它们的方式相同。
我想我慢慢对什么是 monad 有了感觉,但不太确定。 我完全不知道为什么我必须使用lift 函数。
错误信息:
• No instance for (MonadIO
(Web.Scotty.Internal.Types.ScottyT
text-1.2.4.1:Data.Text.Internal.Lazy.Text IO))
arising from a use of ‘liftIO’
• In a stmt of a 'do' block:
paths <- liftIO $ getAllFilePaths2 path
In the expression:
do paths <- liftIO $ getAllFilePaths2 path
pathsToScotty paths
In an equation for ‘pathsToScotty2’:
pathsToScotty2 path
= do paths <- liftIO $ getAllFilePaths2 path
pathsToScotty paths
|
49 | paths <- liftIO $ getAllFilePaths2 path
发生错误的地方:
import Control.Monad.IO.Class
...
pathsToScotty2 :: String -> ScottyM ()
pathsToScotty2 path = do
paths <- liftIO $ getAllFilePaths2 path
pathsToScotty paths
getAllFilePaths2 :: String -> IO [String]
getAllFilePaths2 dir = do
putStrLn dir
isFile <- doesFileExist dir
if isFile
then return [dir]
else do
dirs <- listDirectory dir
foldl foldHelper2 (return []) $ map (\d -> show $ mconcat [dir, "/",d ]) dirs
foldHelper2 :: IO [String] -> String -> IO [String]
foldHelper2 ps path = do
paths <- ps
newPaths <- getAllFilePaths2 path
return (paths ++ newPaths)
【问题讨论】:
-
次要:您误读了错误消息。其形式为
No instance for (MonadIO (X Y Z)),其中X=Web.Scotty.Internal.Types.ScottyT、Y=text-1.2.4.1:Data.Text.Internal.Lazy.Text和Z=IO。在您的问题中,您提到Y Z好像那是一种类型,但事实并非如此。Z是X的第二个参数,而不是Y的第一个参数(不带参数)。 -
ScottyM 必须是 MonadIO 的一个实例。如果它是 IO 上的 monad 转换器,您应该能够推导出
标签: haskell monads io-monad scotty lifting