Search code examples
haskellreflex

Best way to bring the contents of a textfile into a reflex project


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.


Solution

  • 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 a ByteString. However, as you can see below, the type signature indicates a value of type Q Exp will be returned. In order to convert this into a ByteString, you must use Template Haskell syntax, e.g.:

    $(embedFile "myfile.txt")
    

    This expression will have type ByteString.