HP Fortran Programmer's Reference (September 2007)

Program units and procedures
Procedure interface
Chapter 7 185
Defined operators
The OPERATOR clause can be used with the INTERFACE statement either to define a new
user-defined operator or to extend—or
overload
—the behavior of an already defined or
intrinsic operator. This second use is similar to defining a generic procedure (see “Generic
procedures” on page 183). The re-defined operator becomes associated with a
generic
operator
.
When the OPERATOR clause is present in the INTERFACE statement, the specific procedures
within the interface block must all be functions. The functions can implement the operator for
operands of different types, kinds, and ranks. These functions are restricted to one or two
mandatory arguments, depending on whether the defined operator is unary or binary. The
functions return the result of an expression of the form:
[
operand
]
operator operand
Each dummy argument of the functions listed in the interface block must have the
INTENT(IN) attribute. If operator is intrinsic, each specified function must take the same
number of arguments as the intrinsic operator has operands. Furthermore, the arguments
must be distinguishable from those normally associated with the intrinsic operation.
However, argument keywords must not be used when the argument is specified as an operand
to a defined operator.
If a user-defined operator is referenced by its generic name, the reference must resolve to a
unique, specific function name. The selection of the function is accomplished by matching the
number, type, kind, and rank of the operand with the dummy argument lists of the functions
specified in the interface block. As with generic name references (see “Generic procedures” on
page 183), exactly one procedure must match the properties of the operands, and the
matching function is selected and invoked.
The following program, def_op.f90, illustrates a defined operation. The operation, .inrect.,
compares two derived-type operands. The one operand holds the x and y co-ordinates of a
point on a graph, and the other holds the set of co-ordinates defining a rectangle. If the point
is inside the rectangle, the operation evaluates to .TRUE.. The module in which the operation
is defined also contains the definitions of the types of the operands.
As noted in the comments, when a module is defined in the same file as any USE statements
that reference the module, the definition must lexically precede the USE statements. For
information about modules and the USE statement, see “Modules” on page 190.
Example 7-9 def_op.f90
! Note that, if a module definition and any USE statements that
! reference the definition are in the same file, then the
! definition must lexically precede the USE statements.
MODULE coord_op_def
! Defines a logical operation for comparing two derived-type