I am learning Haskell. When I gone through following documentation. https://www.haskell.org/tutorial/classes.html
It is mentioned that "Haskell does not support the C++ overloading style in which functions with different types share a common name." I am not getting this statement, I guess ad-hoc polymorphism (which is done by using type classes) is equivalent to method overloading in C++, Java. Can some body explain me is my understanding correct ?
class Equal a where
isEquals :: a -> a -> 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 where
isEquals emp1 emp2 = getEmpId emp1 == getEmpId emp2
instance Equal Student where
isEquals stud1 stud2 = getStudId stud1 == getStudId stud2
In the above snippet 'isEquals' function is applied to two different types Employee, Student which is equivalant of overloading in C++, Java. Is my understanding correct?
isEqualsfunction - its type isEqual a => a -> a -> Bool. Typeclasses give you much more power than method overloading, so to say they are "equivalent" is wrong. Already yourEqualsclass precludesisEquals :: Int -> Char -> Boolwhich is something you cannot get from simple method overloading. Haskell typeclasses are more similar to OO interfaces - where you define a specification in advance which can be implemented by multiple types (although there are again plenty of differences).