Added default Signatures using Generics #4

wants to merge 1 commit into


None yet

2 participants


Followup to the old email I sent.

Now Default instances can be created like Aeson instances

$ ghci -XDeriveGeneric Data/Default.hs  
>>> import GHC.Generics
>>> data Foo = Bar | Baz | Quux deriving(Show, Generic)
>>> instance Default Foo
>>> def :: Foo

If you are worried about backward compatibility pre 7.4 I can add the #defines and cabal flags necessary for conditional builds.



What is this good for?


Normal version:

data Foo = Bar | Baz
instance Default Foo where def = Bar

Generics version:

{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Foo = Bar | Baz deriving (Generic)
instance Default Foo

I don't think this is easier.


Here is a better example.

  {-# LANGUAGE DeriveGeneric #-}
  import GHC.Generics
  import Data.Default

  data BigRecord = BigRecord {
        foo0  :: Int,
        foo1  :: Int,
        foo2  :: Double,
        foo3  :: (Int, Int),
        foo4  :: Int,
        foo5  :: Int,
        foo6  :: Double,
        foo7  :: (Int, Int),
        foo8  :: Int,
        foo9  :: Int,
        foo10 :: Double,
        foo11 :: (Int, Int),
        foo12 :: Int,
        foo13 :: Int,
        foo14 :: Double,
        foo15 :: (Int, Int),
        foo16 :: Int,
        foo17 :: Int,
        foo18 :: Double,
        foo19 :: (Int, Int)
     deriving(Generic, Show)

  instance Default BigRecord

That's much easier. Also, if you truly don't care about what the default is, it keeps up with modifications with the record. You could use Template Haskell to derive your instance, but Template Haskell adds constraints to the order of your declarations, which is restrictive. So I think generics are better solution when possible, and I would argue in this case they are possible and desirable.

@mauke mauke closed this May 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment