module HAppS.Data.Default
(
Default(defaultValue),
defaultDefaultValue,
DefaultD(..),
defaultProxy,
)
where
import qualified Data.ByteString.Char8 as BSC
import Data.Generics.SYB.WithClass.Basics
import Data.Generics.SYB.WithClass.Instances ()
import Data.Int
import Data.Word
import Foreign.ForeignPtr
class (Data DefaultD a) => Default a where
defaultValue :: a
defaultValue = defaultDefaultValue
defaultDefaultValue :: (Data DefaultD a,Default a) => a
defaultDefaultValue = res
where res = case datarep $ dataTypeOf defaultProxy res of
AlgRep (c:_) ->
fromConstrB defaultProxy (defaultValueD dict) c
r ->
error ("defaultDefaultValue: Bad DataRep: " ++ show r)
data DefaultD a = DefaultD { defaultValueD :: a }
defaultProxy :: Proxy DefaultD
defaultProxy = error "defaultProxy"
instance Default t => Sat (DefaultD t) where
dict = DefaultD { defaultValueD = defaultValue }
instance Default a => Default [a] where
defaultValue = []
instance Default Int where defaultValue = 0
instance Default Int8 where defaultValue = 0
instance Default Int16 where defaultValue = 0
instance Default Int32 where defaultValue = 0
instance Default Int64 where defaultValue = 0
instance Default Word where defaultValue = 0
instance Default Word8 where defaultValue = 0
instance Default Word16 where defaultValue = 0
instance Default Word32 where defaultValue = 0
instance Default Word64 where defaultValue = 0
instance Default Integer where defaultValue = 0
instance Default Float where defaultValue = 0
instance Default Double where defaultValue = 0
instance (Default a, Default b) => Default (Either a b) where
defaultValue = Left defaultValue
instance Default () where
defaultValue = ()
instance (Default a, Default b) => Default (a,b) where
defaultValue = (defaultValue, defaultValue)
instance (Default a, Default b, Default c) => Default (a,b,c) where
defaultValue = (defaultValue, defaultValue, defaultValue)
instance (Default a, Default b, Default c, Default d) => Default (a,b,c,d) where
defaultValue = (defaultValue, defaultValue, defaultValue, defaultValue)
instance Default Char where
defaultValue = 'A'
instance Default a => Default (Maybe a) where
defaultValue = Nothing
instance Default BSC.ByteString where
defaultValue = BSC.pack ""
instance Default a => Default (ForeignPtr a) where
defaultValue = error "defaultValue: ForeignPtr"