Sunday, 1 May 2016

Haskell: Multi-parameter type class

Core Haskell don’t support multi parameter type classes. GHCi provides MultiParamTypeClasses extension, by enabling this you can define multi parameter type class.


CustomType.hs
{-# LANGUAGE MultiParamTypeClasses #-} 
class Equal a b where

    isEquals :: a -> b -> Bool


type Id = Int
type Name = String

data Employee = Engineer Id Name 

data Student = Student Id Name

getEmpId (Engineer empId _) = empId
getStudId (Student studId _) = studId

instance Equal Employee Student where
    isEquals emp1 stud1 = getEmpId emp1 == getStudId stud1

instance Equal Student Employee where
    isEquals stud1 emp2 = getStudId stud1 == getEmpId emp2

instance Equal Student Student where
    isEquals stud1 stud2 = getStudId stud1 == getStudId stud2

instance Equal Employee Employee where
    isEquals emp1 emp2 = getEmpId emp1 == getEmpId emp2



Previous                                                 Next                                                 Home

No comments:

Post a Comment