DEV Community

Discussion on: What the heck is polymorphism?

hiasltiasl profile image
Matthias Perktold • Edited on

Thanks for this!

Regarding subtype polymorphism, I think you can use type classes to achieve something similar.
For instance, the type class for your Vehicle example would look like:

class Vehicle v where
    getWeight :: v -> Int

Each 'subtype' can then be modelled as an instance of Vehicle that implements the getWeight function accordingly.

Now I'm far from a Haskell expert, so what do you think about this relation between subtype polymorphism and type classes?

You can also view it from the other side: If you want to model a Haskell type class in Java, you would use an interface, i.e. subtype polymorphism.

For instance, if we take the Functor type class with the fmap function, we would introduce a Functor<T> interface with an fmap method. Functor instances would then be modelled as classes that implement the Functor interface.

jvanbruegge profile image
Jan van Brügge Author • Edited on

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:

class Semigroup a where
    (<>) :: a -> a -> a

class Semigroup a => Monoid a where
    mempty :: a

and in Java:

public interface Semigroup<T> {
    T combine(T a, T b);

public interface Monoid<T> extends Semigroup<T> {
    T mempty();

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:

class Functor f where
    fmap :: (a -> b) -> f a -> f b

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:

public interface Functor<F<?>> {
    <A,B> F<B> fmap(Function<A, B> f, F<A> x);

which is not valid Java at all.

hiasltiasl profile image
Matthias Perktold

Thanks for the detailed answer!

You are right, even though the two concepts overlap a bit, there is quite some mismatch.