[Haskell-cafe] Re: [Haskell] ANNOUNCE: binary: high performance, pure binary serialisation

Donald Bruce Stewart dons at cse.unsw.edu.au
Thu Jan 25 22:07:53 EST 2007


dons:
> 
>         Binary: high performance, pure binary serialisation for Haskell
>      ---------------------------------------------------------------------- 
> 
> The Binary Strike Team is pleased to announce the release of a new,
> pure, efficient binary serialisation library for Haskell, now available
> from Hackage:

Ok, I forgot one point. It is possible to automatically derive instances
of Binary for your custom types, if they inhabit Data and Typeable,
using an SYB trick. Load tools/derive/BinaryDerive.hs into ghci, and
bring your type into scope, then run:

    *Main> mapM_ putStrLn . lines $ derive (undefined :: Drinks)

To have the source for the Binary instance for the type Drinks derivied
for you:

    *Main> mapM_ putStrLn . lines $ derive (undefined :: Drinks)

    instance Binary Main.Drinks where
      put (Beer a) = putWord8 0 >> put a
      put Coffee = putWord8 1
      put Tea = putWord8 2
      put EnergyDrink = putWord8 3
      put Water = putWord8 4
      put Wine = putWord8 5
      put Whisky = putWord8 6
      get = do
        tag_ <- getWord8
        case tag_ of
          0 -> get >>= \a -> return (Beer a)
          1 -> return Coffee
          2 -> return Tea
          3 -> return EnergyDrink
          4 -> return Water
          5 -> return Wine
          6 -> return Whisky

The use of SYB techniques to provide a 'deriving' script along with a new
typeclass seems to be quite handy.

-- Don


More information about the Haskell-Cafe mailing list