我正在尝试使用Haskell Bson,我想保存和加载它们。保存似乎没问题,但我在Binary.get
功能上遇到打字错误。
这是我的代码:
{-# LANGUAGE GeneralizedNewtypeDeriving, TypeSynonymInstances, FlexibleInstances #-}
module Database.Axiom where
import Data.Bson (Document, Field)
import Data.Bson.Binary (putDocument, getDocument)
import Data.Binary as B (Binary(..), decodeFile, encodeFile)
import Control.Monad (liftM2)
instance Binary Document where
put = putDocument
get = getDocument
data Collection = Collection {
collectionName :: ByteString,
collectionDocs :: [Document]
}
instance Binary Collection where
put (Collection name docs) = B.put name >> B.put docs
get = liftM2 Collection B.get B.get -- < Here is the type error
这导致了这个错误:
Database/Axiom.hs:24:39:
Overlapping instances for Binary [Field]
arising from a use of `B.get'
Matching instances:
instance Binary a => Binary [a] -- Defined in Data.Binary
instance Binary Document -- Defined at Database/Axiom.hs:13:10-24
In the third argument of `liftM2', namely `B.get'
In the expression: liftM2 Collection B.get B.get
In an equation for `get': get = liftM2 Collection B.get B.get
问题在于,Document
它只是 的同义词[Field]
。但我需要一个实例Binary Document
,因为没有函数可以序列化单个Field
. 而且,BSON不会为 导出任何实例Binary Field
,所以我完全不明白为什么首先会发生这个错误。
我尝试使用严格的类型声明,然后使用自制get
方法,但get :: [Document]
只有在有方法时才能很好地工作get :: Document
。
那么,任何人都可以帮助我,也许?