Yeah, you can emulate parts of it with ad-hoc polymorphism, but it remains an approximation because type classes are more like interfaces and not like base classes. You can't do something like super.getWeight().
On the other hands interfaces (at least in Java) are not powerful enough to model type classes either. For simple type classes like Semigroup or Monoid this works perfectly:
The problem is the more complicated type classes like Functor or Monad. More specifically the type classes that use higher kinded types. You see above that the a from the Monoid definition is directly used in the type signature. This means it can only have the kind Type. But take the definition of Functor for example:
classFunctorfwherefmap::(a->b)->fa->fb
The f is not directly used, but it is partially applied to another type. This means that f has kind Type -> Type, ie it is a type constructor. In Java this would mean that you would need a generic generic type, something like this:
Yeah, you can emulate parts of it with ad-hoc polymorphism, but it remains an approximation because type classes are more like interfaces and not like base classes. You can't do something like
super.getWeight()
.On the other hands interfaces (at least in Java) are not powerful enough to model type classes either. For simple type classes like Semigroup or Monoid this works perfectly:
and in Java:
The problem is the more complicated type classes like
Functor
orMonad
. More specifically the type classes that use higher kinded types. You see above that thea
from the Monoid definition is directly used in the type signature. This means it can only have the kindType
. But take the definition ofFunctor
for example:The
f
is not directly used, but it is partially applied to another type. This means thatf
has kindType -> Type
, ie it is a type constructor. In Java this would mean that you would need a generic generic type, something like this:which is not valid Java at all.
Thanks for the detailed answer!
You are right, even though the two concepts overlap a bit, there is quite some mismatch.