- 2.2.16 -
TMT Pascal Language Description
Pascal Language StructureUser Defined Operators
TMT Pascal allows redefining of the standard operators on predefined types
and overloading of these operators for new types. For this, it uses the
construction
overload
The syntax is:
overload op_sign = qualified procedure identifier;
Where the op_sign is one of the standard operator symbols:
+ - / * = <> < > <= >=
and or xor shl shr mod div in not
+:= -:= *:= /:=
When a re-defined operator is used, TMT Pascal uses the last definition
that could be applied toward operands of given types.
For example, this fragment:
function add2_rr (a, b: Real): Real;
Result := (a + b) * 2;
function add2_ii (a, b: Integer): Integer;
Result := (a + b) * 2;
overload + = add_rr;
overload + = add_ii;
redefines the "+" operator. Notice that the order of overload's
is important.
The reverse order
overload + = add_ii;
overload + = add_rr;
will cause add_rr to be used always since integers can always
be cast into reals.
In the SOURCES subdirectory you can find the source of the COMP module
which realizes the complex numbers and defines the operators on them.
Remarks:
- The operators +:=, -:=, *:= and /:= have the lowest precedence
(lower, than the comparison operators) and are right-associative.
- The operators "+:=" and "-:=" are predefined
for all integer and real types.
- The operators "*:=" and "/:=" are predefined
for all real types, with the obvious meaning.
|
|
|
Function Overloading |
Table of Content |
User Defined Reader Procedure |
- 2.2.16 -