【问题标题】:Haskell: Couldn't match type ‘[Char]’ with ‘Text’Haskell:无法将类型“[Char]”与“Text”匹配
【发布时间】:2017-08-28 22:24:24
【问题描述】:

出于某种原因-我希望通过提出这个问题来找出答案-最近对 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)) 一样调整了该行,但我仍然无法构建该项目。

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

完整的文件和添加此功能的进度(相当bug!)很好地封装在this gist file中。

【问题讨论】:

    标签: haskell


    【解决方案1】:

    要将String(即FilePath)转换为Text,您需要显式使用pack:单独使用类型注释是行不通的。

    您还需要使用append(或Monoid(&lt;&gt;))而不是列表特定的(++)

    【讨论】:

    • 酷 - 谢谢你的帮助!你能告诉我那会是什么样子吗?我对 haskell 还是很陌生
    • 我以为 &lt;&gt; 被调到了 Semigroup?
    【解决方案2】:
    when (not pathChecksOut) .
        errorL' . T.pack $ "Failed to access file at : " ++ passPath
    

    正如错误消息所说,“errorL' 的第一个参数”是“预期类型:Text”,但您传递了“实际类型:FilePath”。在这种特殊情况下,您想要转换,所以您 hoogle for FilePath -&gt; Text,如果找不到您想要的内容,请记住 FilePath 是一个类型别名,并且 hoogle for String -&gt; Text 也是。

    【讨论】:

    • man 可能已经修复了它-但现在我收到一个错误,即 OP 函数中的这一行 then (readable &lt;$&gt; getPermissions path)Variable not in scope :/
    • 你已经得到了它(看看你的截图),错误信息告诉你你需要做什么:将它添加到System.Directory的导入列表中。
    猜你喜欢
    • 2022-01-23
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2014-08-30
    相关资源
    最近更新 更多