3

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.

smatthewenglish
  • 2,831
  • 4
  • 36
  • 72

2 Answers2

5

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.

gallais
  • 11,823
  • 2
  • 30
  • 63
2
when (not pathChecksOut) .
    errorL' . T.pack $ "Failed to access file at : " ++ passPath

As the error message says, "the first argument of errorL'" is "Expected type: Text", but you have passed an "Actual type: FilePath". In this particular case, you want to convert, so you hoogle for FilePath -> Text and if that doesn't find what you want you remember that FilePath is a type alias and hoogle for String -> Text also.

  • man might have that fixed it- but now I'm getting an error that this line `then (readable <$> getPermissions path)` in the function of the OP, is `Variable not in scope` :/ – smatthewenglish Aug 28 '17 at 14:45
  • 2
    You were already getting it (look at your screenshot) and the error message tells you exactly what you need to do: add it to the import list of `System.Directory`. – gallais Aug 28 '17 at 18:15