将文本文件的内容带入反射项目的最佳方法
Best way to bring the contents of a textfile into a reflex project
我有一个 70 行的文本文件,我想将其内容作为项目中文本区域的初始值。做这个的最好方式是什么?通常我会使用 readFile
但我似乎无法在这种情况下使用它。
您可以使用模板 Haskell 在编译时加载文件并将其内容存储在顶层定义中。 The file-embed
package on Hackage 为您实现了这个功能:
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
.
我有一个 70 行的文本文件,我想将其内容作为项目中文本区域的初始值。做这个的最好方式是什么?通常我会使用 readFile
但我似乎无法在这种情况下使用它。
您可以使用模板 Haskell 在编译时加载文件并将其内容存储在顶层定义中。 The file-embed
package on Hackage 为您实现了这个功能:
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
.