我需要序列化一個DAWG
(由this library)提供的字節字符串。正如我在文檔中看到有一個實例(Ord b, Binary b) => Binary (DAWG a b)
似乎提供支持,我直接嘗試使用encode
:?序列化類型定義的數據類型
import qualified Data.DAWG.Dynamic as Dawg
import qualified Data.Binary as Bin
Bin.encode $ Dawg.fromList [("foo",1),("bar",2)]
但GHC抱怨有no instance for (Bin.Binary (Dawg.DAWG Char b0))
我得到的,這是必要告訴GHC的數據類型圖形包含,但後來,它是如何完成的點,如果我錯了,我應該怎麼做呢?
編輯: 堆棧跟蹤Bin.encode $ Dawg.fromList [("foo"::String,1::Int),("bar",2)]
:
<interactive>:19:1:
No instance for (Bin.Binary (Dawg.DAWG Char Int))
arising from a use of `Bin.encode'
Possible fix:
add an instance declaration for (Bin.Binary (Dawg.DAWG Char Int))
In the expression: Bin.encode
In the expression:
Bin.encode
$ Dawg.fromList [("foo" :: String, 1 :: Int), ("bar", 2)]
In an equation for `it':
it
= Bin.encode
$ Dawg.fromList [("foo" :: String, 1 :: Int), ("bar", 2)]
沒有完整的錯誤我不能確定,但可能你被咬了,因爲'1'和'2'是多態的。給他們中的一個簽名(例如':: Int'),看看會發生什麼。 –
您是否試過'Bin.encode $ Dawg.fromList [(「foo」,1 :: Int),(「bar」,2 :: Int)]'?我認爲'Binary'需要一個具體的類型來自動序列化數字。 –
我剛試過這個,但是GHC仍然抱怨(雖然它知道圖形類型是'Dawg.DAWG Char Int')。 – michaelmeyer