Skip to content

To Overload or not to Overload


You cannot compare apples with orange!
How many times we heard this sentence when we tried as children to do something apparently illogical?
We're here going to talk about an argument that looks like it: the overloading operation.

Despite of picture beside, the concept is indeed light: it's just a matter of definition, and programming languages usually give us all the possibility to redefine a wider set of expressions for the subject of overloading.

We surely cannot get an apple juice from only orange and vice-versa, but we surely can have one of fruit with both of them: we have just expanded the "making a juice" meaning.
So what about coding?

In this context we make the same, in the sense of a meaning addition: but which is the subject?
Generally every talk about overloading concerns a separation between the two possible macro fields of application:

i)  operators overloading;

ii) functions/procedures overloading.

While this can be useful for specific differences between them two, in reality it could bring a novice to a little confusion. Overloading is the same for both of them; this is the main point, after which we can face every other aspect involved in its particularizations.

A step back.
overload is a reserved word (that is, it cannot be used for any other goal) belonging to the class of modifiers: these are what they mean, modifiers of behavior in subjects which they're applied to.
The kind of modification we operate with the overloading is, as said before, an addition.

For example we are free to define the sum (and modify the operator +) between an integer and a record defined by you.

Even if operators overloading is powerful, it hides some problems that an inexpert programmer can match: if the new definition is incomplete this will probably result in an error during compilation: why this?
Think that A+B is same of B+A in arithmetic we're used to: but in other contexts don't.
Again... integer + record is ok because of our definition; but record + integer? Absolutely no.
You have to cover all possibilities, because compilers thinks in a more rigid way than the one we normally know.

There's another problem as well: the so called exception handling (a technique to prevent undesired situations that the code cannot cover from the beginning) is unusable, while it is for function and procedure.
Moreover every overload to operators can be realized just with functions/procedures, so we show only this situation.
Notice that syntax for overloading of the two cases are different.