Haskell:无法将类型'[Char]'与'Text'匹配

时间:2017-08-28 13:35:49

标签: haskell

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

import System.Directory (doesFileExist, removeFile,getPermissions)

用于促进以下功能:

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

调用为:

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

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

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

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

似乎有一个关于StackOverflow试图解决类似问题this one的问题。为了遵循这个建议我按照errorL' ("Failed to access file at : " ++ (passPath :: Text))调整了这条线,但我仍然无法建立这个项目。

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

enter image description here

完整文件和添加此功能的进度(而不是 bug !)已完全封装在this gist file中。

2 个答案:

答案 0 :(得分:4)

要从String(这是FilePath)转换为Text,您需要明确使用pack:单独使用类型注释不会。

您还需要使用append(或Monoid的{​​{1}})代替列表特定的(<>)

答案 1 :(得分:1)

when (not pathChecksOut) .
    errorL' . T.pack $ "Failed to access file at : " ++ passPath

正如错误消息所示,&#34; errorL'&#34;的第一个参数;是&#34;预期类型:Text&#34;,但您已通过&#34;实际类型:FilePath&#34;。在这种特殊情况下,您想要进行转换,因此您需要对FilePath -> Text进行搜索,如果找不到您想要的内容,请记住FilePath是类型别名并且是String -> Text的hoogle也是。