Haskell:无法将类型“[Char]”与“文本”匹配

sma*_*ish 2 haskell

出于某种原因——我希望通过提出这个问题来找出这一点——最近对 haskell 脚本实施了一系列更新,包括添加:

import System.Directory (doesFileExist, removeFile,getPermissions)
Run Code Online (Sandbox Code Playgroud)

这有助于促进以下功能:

validFile :: FilePath -> IO Bool
validFile path = do
    exists <- (doesFileExist path)
    if exists
    then (readable <$> getPermissions path)
    else return False
Run Code Online (Sandbox Code Playgroud)

调用为:

pwds <- case cfgPasswords of
    Just passPath -> do
         pathChecksOut <- validFile passPath
         when (not pathChecksOut) $ 
             errorL' ("Failed to access file at : " ++ passPath)
         (map (Just . T.unpack) . lines) <$> readFileUtf8 passPath
    Nothing       -> return $ replicate (length cfgPublicKeys) Nothing
Run Code Online (Sandbox Code Playgroud)

我无法再在我的机器上构建项目。

我得到的错误是Couldn't match type ‘[Char]’ with ‘Text’,它指向我以下行:

errorL' ("Failed to access file at : " ++ passPath)

似乎有一个关于 StackOverflow 的问题试图解决一个类似的问题,这个问题。为了遵循该建议,我像这样修改了这条线errorL' ("Failed to access file at : " ++ (passPath :: Text)),但我仍然无法构建该项目。

在实施您在上一篇文章中推荐的更改后,我收到的确切控制台输出如下所示:

在此处输入图片说明

完整文件和添加此功能的进度(而不是错误!)很好地封装在这个 gist 文件中

gal*_*ais 5

要从 a String(这是什么FilePath)转换为 aText您需要显式使用pack:单独的类型注释是行不通的。

您还需要使用append(或Monoid's (<>)) 而不是(++)特定于列表的。