-1

所以我有一个简单的数据结构,它有一些ByteStrings. 我想将这些序列化为 Dhall 文件。但是,我显然不能自动派生ToDhall,因为没有ToDhall. 我该怎么写?

data WordBounds = WordBounds { zipFile :: Prelude.FilePath
                             , start :: BS.ByteString
                             , end :: BS.ByteString
                             } deriving (Show, Generic, ToDhall)

我已经尝试过instance ToDhall BS.ByteString了,我想我越来越近了,但我仍然不太明白 of 的语法instance试图做什么,和/或如何让它与 Dhall 一起工作。

4

1 回答 1

3

从文档中,您可以执行以下操作:

instance ToDhall ByteString where
    injectWith = contramap unpack . injectWith

这将两个现有实例用于ToDhall

instance ToDhall Word8
instance ToDhall a => ToDhall [a]

...所以injectWith等式的 RHS 是 for [Word8],而不是对 for 的递归调用ByteString。它还使用

instance Contravariant Encoder

将 an 转换Encoder [Word8]Encoder ByteString.

也就是说,这将是一个孤儿实例。大多数社区会建议您不要这样做。替代方法包括创建一个newtype, 如

newtype Jonathan'sByteString = Jonathan'sByteString ByteString

并为此编写实例,或者简单地编写

jonathan'sInjectWith :: InputNormalizer -> Encoder ByteString
jonathan'sInjectWith = contramap unpack . injectWith

然后在手写的instance ToDhall WordBounds.

于 2021-09-25T18:46:25.880 回答