Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 849
gatoatigrado Avatar asked Jan 03 '12 15:01

gatoatigrado


3 Answers

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
like image 76
jmr Avatar answered Nov 01 '22 03:11

jmr


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

like image 31
aleator Avatar answered Nov 01 '22 04:11

aleator


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.

like image 39
ehird Avatar answered Nov 01 '22 02:11

ehird