Saturday, 30 April 2016

Haskell: Adding type variables to custom types


Just like we defined multiple type variables to a function arguments, we can also define a type using multiple type variables.
EmployeeUtil.hs
{- Define an employee type -}
data Employee a b c= Engineer {name :: String, engineerId :: a}
                | Manager {name :: String, engineerIds :: [a], managerId :: b}
                | Director {name :: String, managerIds :: [b], directorId :: c}
                deriving Show

*Main> :load EmployeeUtil.hs 
[1 of 1] Compiling Main             ( EmployeeUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> let manager = Manager "Anand" [2, 3, 5] "E432123"
*Main> 
*Main> :t manager
manager :: Num a => Employee a [Char] c



Previous                                                 Next                                                 Home

No comments:

Post a Comment