有没有办法初始化函数
someText :: Text
哪个值将存储在编译时可用的文件中?
我以为我可以使用TH,但现在我只是found
embedFile :: FilePath -> Q Exp
runQ :: Quasi m => Q a -> m a
我只能将Q
打开到IO
:
instance Quasi IO
instance Quasi Q
我想我需要Identity
Quasi的实例,但没有人。
答案 0 :(得分:9)
这不是
someText :: Text
someText = $(embedStringFile "path/to/file")
我错过了什么吗?
(这是TH splice本身在运行时将Q Exp
转换为其他类型。你不应该需要任何类型类实例或任何东西......)
答案 1 :(得分:3)
# foo.txt
barbazblub
module FileText where
import Language.Haskell.TH
fileText :: FilePath -> Q Exp
fileText fp = LitE . StringL <$> runIO (readFile fp)
{-# LANGUAGE TemplateHaskell #-}
module Foo where
import FileText
main = putStrLn $(fileText "foo.txt")
$ runhaskell Foo.hs
barbazblub