The concept of polymorphism has its roots in programming language design and implementation. A language is called polymorphic if functions, procedures and operands are allowed to be of more than one type. In comparison with polymorphic languages, there are languages called monomorphic, such as FORTRAN and C. Being monomorphic means that it is not possible, for example, to define two subroutines in FORTRAN with the same name but different number of parameters.
Overloading is a specific kind of polymorphism which Gamma supports.
Operator overloading allows the programmer to
define new actions for operators (+,-,*,/, etc.) normally associated only
with numbers. For example, the plus operator (+) normally
adds only numeric variables. Operator overloading allows the user to define
an alternate action to adopt when non-numeric variables are used in
conjunction with an operator. The plus operator is often overridden so that
strings may be concatenated using the syntax:
result = "hello" + " " + "there";
When overloading an operator in Gamma the developer must exercise extreme caution since operator overloading is achieved by redefining the operator itself. The typeless quality of variables in Gamma does not allow the interpreter to select an appropriate operator based on the types of the arguments.
Consider the following program fragment:
// Assign plus to a function called 'real_plus'.
real_plus = \+;
// Re_define plus to check for strings, and call
// string() or real_plus() depending on arg types.
function \+ (arg1, arg2)
{
if (string_p(arg1) || string_p(arg2))
string(arg1,arg2);
else
real_plus(arg1,arg2);
}
The first step in this example is to re-assign the functionality of the
+ operator to a function called
real_plus. Notice that the backslash character is
used to pass the + character explicitly. Without the
backslash Gamma would interpret the plus character in the function
definition statement as a mistake in syntax.
Once this assignment and definition are entered into Gamma the plus operator can be used with strings as well as with numbers:
Gamma>5 + 4;9Gamma>"hello" + " " + "there";"hello there"
While it is convenient to set up overloaded functions in Gamma, remember that user functions are generally slower than Gamma's built-in functions.