Reputation: 113
I'm trying to write a function that loads all files in a directory into a [ByteString], by calling loadFile on each element in the list that comes from getDirectoryContents
Right now I've got this:
import Data.ByteString
import System.Directory
import System.IO
import Control.Monad
loadFile :: String -> IO B.ByteString
loadFile fileName = withFile fileName ReadMode (\handle -> hGetContents handle)
loadFiles :: String -> IO [IO B.ByteString]
loadFiles x = return (map (loadFile) $ getDirectoryContents x)
And I'm getting this error:
main.hs:28:60:
Couldn't match type `[Char]' with `B.ByteString'
Expected type: IO B.ByteString
Actual type: IO String
In the return type of a call of `hGetContents'
In the expression: hGetContents handle
In the third argument of `withFile', namely
`(\ handle -> hGetContents handle)'
I am very new to haskell (I started learning yesterday) so this error message is confusing to me. I expected it to infer the type B.ByteString since that is part of the return value of loadFile. Also any comments on style/convention are welcome
Upvotes: 0
Views: 273
Reputation: 15038
You are using hGetContents
from System.IO
, which returns an IO String
. You probably want to be using B.hGetContents
from Data.ByteString
, which returns an IO ByteString
.
The reason I know this is because the error message says it expects IO B.ByteString
, but it has IO String
, "In the return type of a call of hGetContents
."
Upvotes: 1