[commit: packages/binary] master: Update README's example about deriving instances (cb344c2)
git at git.haskell.org
git at git.haskell.org
Sun Dec 14 17:55:07 UTC 2014
Repository : ssh://git@git.haskell.org/binary
On branch : master
Link : http://git.haskell.org/packages/binary.git/commitdiff/cb344c27889e5cbb42dad36e2c6f1780b3c9cea2
>---------------------------------------------------------------
commit cb344c27889e5cbb42dad36e2c6f1780b3c9cea2
Author: Lennart Kolmodin <kolmodin at gmail.com>
Date: Sun May 11 16:13:00 2014 +0400
Update README's example about deriving instances
Demonstrate how to use GHC's Generics.
>---------------------------------------------------------------
cb344c27889e5cbb42dad36e2c6f1780b3c9cea2
README.md | 48 ++++++++++++++----------------------------------
1 file changed, 14 insertions(+), 34 deletions(-)
diff --git a/README.md b/README.md
index 5234835..b32d7ff 100644
--- a/README.md
+++ b/README.md
@@ -54,42 +54,22 @@ the ``Get`` and ``Put`` monads.
More information in the haddock documentation.
-## Deriving binary instances ##
+## Deriving binary instances using GHC's Generic ##
-It is possible to mechanically derive new instances of Binary for your
-types, if they support the Data and Typeable classes. A script is
-provided in tools/derive. Here's an example of its use.
+Beginning with GHC 7.2, it is possible to use binary serialization without
+writing any instance boilerplate code.
- $ cd binary
- $ cd tools/derive
-
- $ ghci -fglasgow-exts BinaryDerive.hs
-
- *BinaryDerive> :l Example.hs
-
- *Main> deriveM (undefined :: Exp)
-
- instance Binary Main.Exp where
- put (ExpOr a b) = putWord8 0 >> put a >> put b
- put (ExpAnd a b) = putWord8 1 >> put a >> put b
- put (ExpEq a b) = putWord8 2 >> put a >> put b
- put (ExpNEq a b) = putWord8 3 >> put a >> put b
- put (ExpAdd a b) = putWord8 4 >> put a >> put b
- put (ExpSub a b) = putWord8 5 >> put a >> put b
- put (ExpVar a) = putWord8 6 >> put a
- put (ExpInt a) = putWord8 7 >> put a
- get = do
- tag_ <- getWord8
- case tag_ of
- 0 -> get >>= \a -> get >>= \b -> return (ExpOr a b)
- 1 -> get >>= \a -> get >>= \b -> return (ExpAnd a b)
- 2 -> get >>= \a -> get >>= \b -> return (ExpEq a b)
- 3 -> get >>= \a -> get >>= \b -> return (ExpNEq a b)
- 4 -> get >>= \a -> get >>= \b -> return (ExpAdd a b)
- 5 -> get >>= \a -> get >>= \b -> return (ExpSub a b)
- 6 -> get >>= \a -> return (ExpVar a)
- 7 -> get >>= \a -> return (ExpInt a)
- _ -> fail "no decoding"
+```haskell
+{-# LANGUAGE DeriveGeneric #-}
+
+import Data.Binary
+import GHC.Generics (Generic)
+
+data Foo = Foo deriving (Generic)
+
+-- GHC will automatically fill out the instance
+instance Binary Foo
+```
## Contributors ##
More information about the ghc-commits
mailing list