Spookless Typeclasses

Everyone else on gemini seems to be doing so at the moment, so I thought I would also give my response to Drew DeVault's article on overloading.

Drew DeVault: Spooky code at a distance

On one hand I agree that this can be horrible, and it's one thing that really put me off C++. On another, a form of overloading is also a feature of Haskell, and in that form I find it entirely non-horrible. So I thought it worth discussing Haskell's approach and what makes it unobjectionable. I am not an expert on any of this.

Typeclasses

In Haskell, the type of the + operator is

(+) :: Num b => b -> b -> b

which can be read as "if b is a type of class Num, then + applied to two instances of b will return an instance of b". Num is a "typeclass", a family of types. You can define a type to be of typeclass Num by giving a definition of + (and some other things). This is a kind of overloading: the definition of + on one type of class Num can be entirely different from that on another.

So why is this less objectionable than the C++ version? I see a few reasons.

Pithy summary

Typeclasses exorcise overloading.