繁体   English   中英

Haskell:无法将类型'[Char]'与'文本'相匹配

[英]Haskell: Couldn't match type ‘[Char]’ with ‘Text’

由于某些原因-我希望通过提出这个问题来找到答案-最近实施了一批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的问题,试图解决类似的问题, 这个问题。 为了遵循该建议,我errorL' ("Failed to access file at : " ++ (passPath :: Text))了类似的行errorL' ("Failed to access file at : " ++ (passPath :: Text)) ,但仍然无法构建项目。

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

在此处输入图片说明

完整文件和添加此功能的进度(相当容易出错 !)已很好地封装在此gist文件中

要将String (即FilePathString )转换为Text您需要显式使用pack :单独的类型注释将不起作用。

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

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

如错误消息所述,“ errorL'的第一个参数”是“期望类型: Text ”,但是您已经传递了“实际类型: FilePath ”。 在这种情况下,您要进行转换,因此请为FilePath -> Text拼凑,如果找不到所需内容,则请记住FilePath是类型别名,也请为String- String -> Text拼凑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM