5.4.24  Overloaded Operators

The alert reader may have noticed that the Mythryl arithmetic operators such as + and * are anomalous in that they can operate upon both integer and floating point numbers.

This is indeed an anomaly.

Such symbols are termed overloaded. They actually refer ambiguously to several underlying symbol definitions.

Such overloading works quite naturally in languages like C++ where every identifier is explicitly assigned a type by the programmer when it enters scope.

Such overloading does not sit very well with Mythryl type inference, alas. Mythryl type inference works by propagating type information out from symbols with known type through the rest of the program syntax tree. Symbols which may refer to any one of several underlying definitions and thus potentially have any one of several types add unwelcome complexity and opacity to the type inference computation.

This is why (for example) the closely related Ocaml programming language abjures overloading completely. The price it pays for this is having to use different symbols for integer and floating point multiplcation — * vs *. — which some people consider ugly. It gets uglier if one wants to add such support for other types such as complex numbers, vectors and matrices.

Consequently Mythryl has elected to support operator overloading, albeit without great enthusiasm or conviction. You might think of operator overloading as the goto of the Mythryl type and syntax system, somewhat sinister and unwelcome, and by preference not mentioned or used, but occasionally just exactly the right tool for the job.

Important note: The Mythryl compiler resolves overloaded operators at compiletime; any given reference in source code to an overloaded operator must resolve at compiletime to exactly one of the underlying possibilities. This means that one cannot use overloaded operators to write a function which will operate on different types at different times, for example a function which does integer additions when handed integers and float additions when handed floats.

You can find the Mythryl standard library’s set of default operator overloading declarations in the aptly named src/lib/core/init/pervasive.pkg package. A typical sample looks like:

    overloaded my + :   ((X, X) -> X)
        ( tagged_int::(+),

This declares that the overloaded + binary operator combines two values to produce a third, all of the same type, and that it may be used to ambiguously refer to any of the corresponding underlying addition symbols from any of the indicated arithmetic-type packages — and to strings too, just for good measure.

You might use the following syntax to add support for complex, quaternion, oction and matrix addition to the pre-existing overloaded addition operation:

    overloaded my + :   ((X, X) -> X)
        ( complex::(+),

Here the substitution of += for = signals the compiler that the pre-existing overloaded operator definition should be extended rather than overridden.

Comments and suggestions to: