收到Key a
类型的值后,我可以轻松定义并使用此功能:
keyToInt64 :: Key a -> Int64
keyToInt64 (Key (PersistInt64 n)) = n
keyToInt64 _ = error "wrong database type"
但是,我不能在Key a
!
instance Num (Key a) where
fromInteger = fromIntegral . keyToInt64
我收到此错误:
Illegal type synonym family application in instance: Key a
In the instance declaration for `Num (Key a)'
如何定义此实例?为什么我的方法不起作用?
答案 0 :(得分:1)
Key entity
是KeyBackend backend entity
的同义词,因此您需要在具体类型上定义实例。
instance Num (KeyBackend backend entity) where
Key (PersistInt64 a) + Key (PersistInt64 b) = Key . PersistInt64 $ a + b
Key _ + _ = error "wrong database type"
_ + Key _ = error "wrong database type"
...
尽管如此无偿地使用error
可能会在以后引起很多痛苦。