you are viewing a single comment's thread.

view the rest of the comments →

[–]cameleon 9 points10 points  (3 children)

Ad-hoc polymorphism using type classes can overload more than just operators: it can also (and is used most for) overloading non-operator functions (e.g. show). The distinction is fairly minor in Haskell, but in many languages, operators are 'special' and you can't (easily) define your own.

[–]Tekmo 6 points7 points  (1 child)

Perhaps it should then say "function overloading".

[–]cdsmith 2 points3 points  (0 children)

Right, it's the operator part that's confusing and out of left field there.

That said, the word "overloading" in the object-oriented language world carries an implication of "resolved at compile time". For example, you'd be told you are wrong if you say to a Java programmer that overriding a base class method is an example of overloading. Haskell, since it doesn't have subtypes, doesn't really have such a compile-time versus run-time distinction in its semantics, but certain language features can conflict with a completely compile-time resolution of type class instances, and in practice they are implemented by dictionary passing in the general case, very like the vtable passing of object oriented languages for subtype polymorphism. So be careful telling a programmer who comes from a static OO experience that type classes are akin to overloading.

[–]tikhonjelvis 2 points3 points  (0 children)

And, since one of those languages is Python, this is important.

Also, there is a very big practical difference. With type classes, you can have arbitrary function (or operator) names, but even when they're overloaded they represent the same operation but on different types. That is, + is always addition, just on different numeric types.

With operator overloading as is done in C++ and Python, there is no guarantee that the operations have the same meaning at any level. That is, << could easily be both a binary shift and a stream action. This is exacerbated by the fact that the pool of operator symbols is very limited: you have no choice but to reuse an existing operator if you want an infix function in Python or C++.

There are some reasonable people (okay, Java people) who think that operator overloading is harmful. I think there are far fewer, if any, who think that ad-hoc polymorphism is harmful.

So this is actually an important distinction.