簡體   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