Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default Signatures using Generics #4

Closed
wants to merge 1 commit into from

Conversation

jfischoff
Copy link

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
Bar

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

Best,
Jonathan

@mauke
Copy link
Owner

mauke commented Mar 31, 2013

What is this good for?

@jfischoff
Copy link
Author

It makes it easier to make instances.

Sent from my iPhone

On Mar 30, 2013, at 11:44 PM, mauke notifications@github.com wrote:

What is this good for?


Reply to this email directly or view it on GitHub.

@mauke
Copy link
Owner

mauke commented Mar 31, 2013

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.

@jfischoff
Copy link
Author

It is if you are already using generics.

Sent from my iPhone

On Mar 31, 2013, at 8:31 AM, mauke notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHub.

@jfischoff
Copy link
Author

Also adding default signatures doesn't mean you have to use them.

Sent from my iPhone

On Mar 31, 2013, at 8:31 AM, mauke notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHub.

@jfischoff
Copy link
Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants