Yesod /永久实体派生Show [英] Yesod/Persistent entity deriving Show
问题描述
在Yesod书的持久性章节中,给出了一个示例,其中该实体
In the Persistent chapter of the Yesod book, an example is given where this entity
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)
mkPersist sqlSettings [persist|
Person
name String
age Int
deriving Show
|]
生成代码
{-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.Store
import Database.Persist.Sqlite
import Database.Persist.GenericSql.Raw (SqlBackend)
import Database.Persist.EntityDef
import Control.Monad.IO.Class (liftIO)
import Control.Applicative
data Person = Person
{ personName :: String
, personAge :: Int
}
deriving (Show, Read, Eq)
type PersonId = Key Person
instance PersistEntity Person where
-- A Generalized Algebraic Datatype (GADT).
-- This gives us a type-safe approach to matching fields with
-- their datatypes.
data EntityField Person typ where
PersonId :: EntityField Person PersonId
PersonName :: EntityField Person String
PersonAge :: EntityField Person Int
type PersistEntityBackend Person = SqlBackend
toPersistFields (Person name age) =
[ SomePersistField name
, SomePersistField age
]
fromPersistValues [nameValue, ageValue] = Person
<$> fromPersistValue nameValue
<*> fromPersistValue ageValue
fromPersistValues _ = Left "Invalid fromPersistValues input"
-- Information on each field, used internally to generate SQL statements
persistFieldDef PersonId = FieldDef
(HaskellName "Id")
(DBName "id")
(FTTypeCon Nothing "PersonId")
[]
persistFieldDef PersonName = FieldDef
(HaskellName "name")
(DBName "name")
(FTTypeCon Nothing "String")
[]
persistFieldDef PersonAge = FieldDef
(HaskellName "age")
(DBName "age")
(FTTypeCon Nothing "Int")
[]
为什么将 derive Show
添加到Person实体会生成所有三个类型类(Show,Read,Eq)
的派生?我是Haskell和Yesod的新手,因此很抱歉,不过我在任何地方都找不到答案!这只是文档中的错误吗?谢谢!
Why does adding deriving Show
to the Person entity generate the derivation of all three typeclasses (Show, Read, Eq)
? I'm very new to Haskell and Yesod, so I apologize if it's obvious, but I can't find the answer anywhere! Is it just an error in the documentation? Thanks!
推荐答案
简单:这是本书中的错字:)。如果查看实际生成的代码(带有 -ddump-splices
),您会看到它实际上只是派生 Show
实例。
Easy: it's a typo in the book :). If you look at the actual generated code (with -ddump-splices
), you'll see that it's only actually deriving the Show
instance.
这篇关于Yesod /永久实体派生Show的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!