-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Extend Haskell data or newtype like in OOP languages -- -- This package allows you to extend Haskell data or newtype like in OOP -- languages. @package data-extend-generic @version 0.1.0.0 module Data.Extend.Internal class GExtend a gExtend :: GExtend a => a p -> a p -> a p class Extend a where extend a b = to $ gExtend (from a) (from b) -- | By default -- --
-- a `extend` b = a ---- --
-- Nothing `extend` Just a = Just a ---- -- To use the Extend class, simply make your data derive Generic. -- -- If "a" is a user defined data type, then all Nothing fields of -- "a" are replaced by corresponding fields in "b", -- -- ie, all Just fields in "a" will override corresponding fields -- in "b". extend :: Extend a => a -> a -> a -- | By default -- --
-- a `extend` b = a ---- --
-- Nothing `extend` Just a = Just a ---- -- To use the Extend class, simply make your data derive Generic. -- -- If "a" is a user defined data type, then all Nothing fields of -- "a" are replaced by corresponding fields in "b", -- -- ie, all Just fields in "a" will override corresponding fields -- in "b". extend :: (Extend a, Generic a, GExtend (Rep a)) => a -> a -> a instance (Data.Extend.Internal.GExtend (GHC.Generics.K1 i (m a)), GHC.Base.Alternative m, Data.Extend.Internal.GExtend b) => Data.Extend.Internal.GExtend (GHC.Generics.K1 i (m a) GHC.Generics.:*: b) instance Data.Extend.Internal.GExtend GHC.Generics.U1 instance Data.Extend.Internal.Extend a => Data.Extend.Internal.GExtend (GHC.Generics.K1 i a) instance (Data.Extend.Internal.GExtend a, Data.Extend.Internal.GExtend b) => Data.Extend.Internal.GExtend (a GHC.Generics.:*: b) instance (Data.Extend.Internal.GExtend a, Data.Extend.Internal.GExtend b) => Data.Extend.Internal.GExtend (a GHC.Generics.:+: b) instance Data.Extend.Internal.GExtend a => Data.Extend.Internal.GExtend (GHC.Generics.M1 i c a) instance Data.Extend.Internal.Extend a => Data.Extend.Internal.Extend (GHC.Base.Maybe a) instance Data.Extend.Internal.Extend a -- | This package allows you to extend a Haskell data like how you do in -- OOP. -- -- Here is an example use for testing: -- --
-- data Test = Test | TestA {
-- f1 :: Int,
-- f2 :: Maybe Int,
-- f3 :: Maybe Int
-- } deriving (Show, Generic, Eq)
--
-- test1A = TestA 1 (Just 1) (Just 3)
-- test2A = TestA 0 Nothing (Just 2)
--
-- main :: IO ()
-- main = hspec $
-- describe "Data.Extend" $ do
-- specify "Int" $
-- (2 `extend` 1) `shouldBe` (2 :: Int)
-- specify "String" $
-- ("b" `extend` "a") `shouldBe` "b"
-- specify "data 0" $
-- (Test `extend` Test) `shouldBe` Test
-- specify "data 1" $
-- (test2A `extend` test1A) `shouldBe` TestA 0 (Just 1) (Just 2)
--
module Data.Extend
class Extend a where extend a b = to $ gExtend (from a) (from b)
-- | By default
--
-- -- a `extend` b = a ---- --
-- Nothing `extend` Just a = Just a ---- -- To use the Extend class, simply make your data derive Generic. -- -- If "a" is a user defined data type, then all Nothing fields of -- "a" are replaced by corresponding fields in "b", -- -- ie, all Just fields in "a" will override corresponding fields -- in "b". extend :: Extend a => a -> a -> a -- | By default -- --
-- a `extend` b = a ---- --
-- Nothing `extend` Just a = Just a ---- -- To use the Extend class, simply make your data derive Generic. -- -- If "a" is a user defined data type, then all Nothing fields of -- "a" are replaced by corresponding fields in "b", -- -- ie, all Just fields in "a" will override corresponding fields -- in "b". extend :: (Extend a, Generic a, GExtend (Rep a)) => a -> a -> a