我正试图让我的脑袋缠绕在Persistent上,我正在努力学习的其中一件事就是derivePersistField
模板haskell功能。现在,我意识到有一个阶段限制,迫使GHC渲染quasiquoter应该生成的其余代码,但我只是没有完成它。 GHC一直抱怨函数调用在顶层,但我不知道生成实际数据类型+好位。这是我的代码(直接来自于持久性的Yesod书):
- Employment.hs ----
module Employment where
import Database.Persist.TH
data Employment = Employed | Unemployed | Retired
deriving (Show, Read, Eq)
derivePersistField "Employment"
- main.hs ----------
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell,
OverloadedStrings, GADTs, FlexibleContexts #-}
import Database.Persist.Sqlite
import Database.Persist.TH
import Employment
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name String
employment Employment
|]
main = runSqlite ":memory:" $ do
runMigration migrateAll
insert $ Person "Bruce Wayne" Retired
insert $ Person "Peter Parker" Unemployed
insert $ Person "Michael" Employed
我还缺少什么?我是否还需要在模块中放置语言扩展?任何帮助将不胜感激!
答案 0 :(得分:2)
derivePersistField
是模板Haskell拼接,如share
,因此为了在模块中使用它,您需要启用TemplateHaskell
语言编译指示。即。
{-# LANGUAGE TemplateHaskell #-}
module Employment where
import Database.Persist.TH
data Employment = Employed | Unemployed | Retired
deriving (Show, Read, Eq)
derivePersistField "Employment"
您还可以通过
启用整个项目的扩展程序extensions: TemplateHaskell
在项目的.cabal
- 文件中,但推荐的方法是在每个模块的基础上启用所需的扩展。