Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added a default signature with generic deriving for Aeson like convience

  • Loading branch information...
commit 8c5b37a8f15ab3c95183de50d9021c13603f5359 1 parent e73e0f2
@jfischoff authored
Showing with 36 additions and 3 deletions.
  1. +34 −1 Data/Default.hs
  2. +2 −2 data-default.cabal
View
35 Data/Default.hs
@@ -28,7 +28,11 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}
-
+{-# LANGUAGE DefaultSignatures #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
module Data.Default (
-- | This module defines a class for types with a default value. Instances are
-- provided for '()', 'S.Set', 'M.Map', 'Int', 'Integer', 'Float', 'Double',
@@ -49,11 +53,14 @@ import Data.Sequence (Seq)
import Data.Tree (Tree(..))
import Data.DList (DList)
import System.Locale
+import GHC.Generics
-- | A class for types with a default value.
class Default a where
-- | The default value for this type.
def :: a
+ default def :: (Generic a, GDefault (Rep a)) => a
+ def = to gDef
instance Default (S.Set v) where def = S.empty
instance Default (M.Map k v) where def = M.empty
@@ -105,3 +112,29 @@ instance (Default a, Default b, Default c, Default d, Default e, Default f) => D
instance (Default a, Default b, Default c, Default d, Default e, Default f, Default g) => Default (a, b, c, d, e, f, g) where def = (def, def, def, def, def, def, def)
instance Default TimeLocale where def = defaultTimeLocale
+
+-- Generic Default for default implementation
+class GDefault f where
+ gDef :: f a
+
+instance GDefault U1 where
+ gDef = U1
+
+instance (Datatype d, GDefault a) => GDefault (D1 d a) where
+ gDef = M1 gDef
+
+instance (Constructor c, GDefault a) => GDefault (C1 c a) where
+ gDef = M1 gDef
+
+instance (Selector s, GDefault a) => GDefault (S1 s a) where
+ gDef = M1 gDef
+
+instance (Default a) => GDefault (K1 i a) where
+ gDef = K1 def
+
+instance (GDefault a, GDefault b) => GDefault (a :*: b) where
+ gDef = gDef :*: gDef
+
+instance (GDefault a, GDefault b) => GDefault (a :+: b) where
+ gDef = L1 gDef
+
View
4 data-default.cabal
@@ -1,5 +1,5 @@
Name: data-default
-Version: 0.5.1
+Version: 0.5.2
Cabal-Version: >= 1.6
Category: Data
Synopsis: A class for types with a default value
@@ -14,6 +14,6 @@ source-repository head
location: https://github.com/mauke/data-default
Library
- Build-Depends: base >=2 && <5, containers, dlist, old-locale
+ Build-Depends: base >=2 && <5, containers, dlist, old-locale, ghc-prim
Exposed-Modules: Data.Default
Ghc-Options: -Wall -O2
Please sign in to comment.
Something went wrong with that request. Please try again.