Overloaded Operators
Operators are Overloaded
User Defined Overloaded Operators
- Users can define functions whose names are strings that are operators
- Example:
with ada.Integer_text_io; use ada.Integer_text_io;
procedure tryOp is
function "+"(i: Integer; f: Float) return integer is
begin
return i + (Integer(f));
end "+";
begin
put("+"(2, 3.0));
put(2 + 3.0);
end tryOp;
Functions that are operators can be used in prefix mode or infix mode
Functions that are operators can be defined for any type (eg arrays, records)
See package standard for declarations of integer operators
What Operators Can be Overloaded
- What are the Ada operators
- abs and mod not or rem xor
- = /= < <= > >=
- + - * / ** &
- + and - can be defined as unary or binary (ie with one or two parameters), or both
- If a definition of "=" returns a boolean, then "/=" is implicitly defined (and cannot
be redefined).
- "/=" can only be redefined if it returns a non-boolean
- Technically the following are NOT operators and can't be redefined:
- in, not in, and then, or else
Overloaded Operators and Packages
- It is very common for a package to contain type definitions
- It is also very common for a package to contain to define operators that operate
on those types
- When a package redefines operators, those operators can be used as infix
operators if client has a use statement for that package
- Ada 95 introduced a new statement (the
use type
statement)
that allows clients to use infix operators, without making the procedures available
without the qualified name.
- Example needed here:
Overloaded Operators and Other Langauges