Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE DeriveGeneric, DefaultSignatures #-}
- {-# LANGUAGE TypeOperators #-}
- {-# LANGUAGE FlexibleContexts, FlexibleInstances #-}
- {-# LANGUAGE ScopedTypeVariables, RankNTypes #-}
- import Generics.Deriving
- -- no value implies no default value
- -- instance Default' V1 where
- -- no fields to default
- instance Default' U1 where
- def' = U1
- -- default on this field
- instance (Default a) => Default' (K1 R a) where
- def' = K1 def
- -- default recursively on each field
- instance (Default' f, Default' g) => Default' (f :*: g) where
- def' = def' :*: def'
- instance (Default' f) => Default' (M1 S s f) where
- def' = M1 def'
- instance (Default' f) => Default' (M1 C c f) where
- def' = M1 def'
- -- the leftmost term is the first constructor
- instance (Default' f) => Default' (f :+: g) where
- def' = L1 def' -- order is guaranteed, nesting is not guaranteed
- instance (Default' f) => Default' (M1 D t f) where
- def' = M1 def'
- class Default' f where
- def' :: f a
- class Default a where
- def :: a
- default def :: (Generic a, Default' (Rep a)) => a
- def = to (def' :: Rep a x)
- data X = A Integer String | B Bool deriving (Generic, Show)
- instance Default X
- instance Default Integer where def = 0
- instance Default String
- main = do
- print $ (def :: X)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement