Instead of thinking of overloaded functions as many functions with the same name that have to be distinguished by their arguments, think of function overloading as providing multiple definitions for one function, with the correct implementation being chosen based on the arguments.
Scripting languages generally don't do function overloading. Instead, you have to manually mimic function overloading by switching on the arguments' types at run-time, viz.
function isEmpty($object)
{
if (is_scalar($object)) return
strlen($object) == 0;
if (is_array ($object)) return count
($object) == 0;
if
(is_null ($object)) return false;
if (is_object($object)) return false;
return !!$object;
}
In a language that does have function overloading, the definition of
isEmpty could be split into multiple functions, each with the same
name but accepting different parameter types.
That makes it complicated to refer to those functions by name since name alone does not uniquely identify one of them. But if function overloading were thought of as syntactic sugar for the above case of a function switching on the type of its arguments, then there would be no difficulty since there would be only one function still. Or, alternatively, you could think of the function name implicitly referencing all of the overloaded functions, with the correct one being selected at call time.
Hmm. But what happens when we want to add a new class which can also have isEmpty applied to it? Do we have to go back and edit this same function and add an extra
if (is_my_class($object))
clause? If I have a hundred classes, all of which can overload an operator like + does that mean the + function will have a hundred armed case statement?
Not at all, Phil. Obviously that would be pretty terrible.
My thought is that functions are special case objects; where classes and variables are identified by name alone, functions often need more of the type signature to identify a specific function among a set of overloaded functions.
I understand the reason for this, but it would be nice to be able to refer
to a function solely by name alone, to have the name uniquely identify the
function. It seems that if overloading a function were treated as syntactic
sugar for a single function with a big switch/case
statement, this special case could disappear.
As to why this would be a good thing—I know I had reasons for this, but at the moment I'm too tired to scour my foggy memory for them.
Welcome to my humble site, Mr. Phil Jones! Apologies in advance if my scribblings are at times opaque; unpaved pathways and overgrown dead-ends are not uncommon here.