I have a 70 line text file, whose contents I want to have as the initial value of a text area within my project. What is the best way to do this? Normally I would use readFile
but I can't seem to use it in this context.
You can use Template Haskell to load the file at compile time and store its contents in a toplevel definition. The file-embed
package on Hackage implements this functionality for you:
This module uses Template Haskell. Following is a simplified explanation of usage for those unfamiliar with calling Template Haskell functions.
The function
embedFile
in this modules embeds a file into the executable that you can use it at runtime. A file is represented as aByteString
. However, as you can see below, the type signature indicates a value of typeQ Exp
will be returned. In order to convert this into aByteString
, you must use Template Haskell syntax, e.g.:$(embedFile "myfile.txt")
This expression will have type
ByteString
.