我正在玩一个可扩展的序列化库的想法。我有以下类型类:
class Monoid m => BuilderS m a where
cstr :: String -> a -> m
这个想法是人们可以为成对的不同序列化器/类型定义实例,如下所示:
import qualified Data.Serialize.Builder as B
instance Serialize a => BuilderS B.Builder a where
cstr _ = B.fromByteString . encode
还有一个使用示例:
sfPut :: (BuilderS a b, Monoid a) => WriterT a Identity ()
sfPut = do
tell $ cstr "version" (5 :: Int)
-- ...
return ()
但是,事实证明a
需要专门化的类型:
Could not deduce (BuilderS a Int) arising from a use of `cstr'
from the context (BuilderS a b, Monoid a)
bound by the type signature for
sfPut :: (BuilderS a b, Monoid a) => WriterT a Identity ()
即,以下类型签名可以正常工作:
sfPut :: WriterT B.Builder Identity ()
有没有明显的方法可以解决这个问题?