Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

For some reason- which I'm hoping to find out by virtue of asking this question- having recently implemented a cadre of updates to a haskell script, including the addition of:

import System.Directory (doesFileExist, removeFile,getPermissions)

which serves to facilitate the following function:

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

invoked as:

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

I'm not able to anymore build the project on my machine.

The error I got was Couldn't match type ‘[Char]’ with ‘Text’, and it pointed me to the following line:

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

It seems there's a question about StackOverflow that tries to address a similar issue, this one. In an attempt to follow that advice I adapted the line like so errorL' ("Failed to access file at : " ++ (passPath :: Text)), but still I wan't able to build the project.

The exact console output I received after implementing the changes you recommend in your last post looks like this:

enter image description here

The full file and the progress of adding this feature (rather bug!) is well encapsulated in this gist file.

like image 642
smatthewenglish Avatar asked May 30 '26 14:05

smatthewenglish


1 Answers

To convert from a String (which is what FilePath is) to a Text you need to explicitly use pack: a type annotation alone won't do.

You will also need to use append (or Monoid's (<>)) instead of (++) which is list specific.

like image 66
gallais Avatar answered Jun 02 '26 09:06

gallais



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!