Is there a Template Haskell / deriving mechanism for Data.Binary (or friends?)
Asked Answered
T

3

7

The Data.Binary documentation shows writing an instance by hand. Is there a way around this? I saw here there is another library, SerTH, which has a (Template Haskell based) deriving mechanism, but the link to it seems broken. Also, if you know other libraries, good performance is critical for us.

Thank you in advance!

Twilley answered 3/1, 2012 at 15:39 Comment(1)
GHC 7.2 supports this natively using default deriving... I wrote the code for it awhile back and it was pretty simpleRambouillet
A
7

See http://hackage.haskell.org/packages/archive/binary/0.7.1.0/doc/html/Data-Binary.html#g:3

 {-# LANGUAGE DeriveGeneric #-}

 import Data.Binary
 import GHC.Generics (Generic)

 data Foo = Foo
          deriving (Generic)

 -- GHC will automatically fill out the instance
 instance Binary Foo
Ancestor answered 2/5, 2013 at 0:14 Comment(0)
C
5

Neil Mitchells Derive package has a template haskell macro for deriving binary instances.

Celloidin answered 3/1, 2012 at 16:5 Comment(0)
H
5

Since you asked about other libraries:

The cereal data serialisation library has cereal-derive, which works with the new Generics support in GHC 7.2. This has a compile-time speed advantage over Template Haskell (I tend to avoid TH these days just because it makes compilation even slower) and a run-time speed advantage over datatype-generic methods like SYB and Uniplate.

cereal is very similar to binary, but uses strict ByteStrings; binary hasn't been updated since 2009 and cereal has niceties such as IEEE-754 float format support, so I can't see any reason not to use it over binary if you want deriving.

Hauberk answered 3/1, 2012 at 18:10 Comment(2)
cereal-derive is GPL3. This might be an issue for some use-cases.Merri
lazy bytestrings are easier for some use casesNeutrophil

© 2022 - 2024 — McMap. All rights reserved.