02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Defined operators<br />

Program units and procedures<br />

Procedure interface<br />

The OPERATOR clausecanbeusedwiththeINTERFACE statement either to define a new<br />

user-defined operator or to extend—or overload—the behavior of an already defined or<br />

intrinsic operator. This second use is similar to defining a generic procedure (see “Generic<br />

procedures” on page 151). The re-defined operator becomes associated with a generic<br />

operator.<br />

When the OPERATOR clause is present in the INTERFACE statement, the specific procedures<br />

within the interface block must all be functions. The functions can implement the operator for<br />

operands of different types, kinds, and ranks. These functions are restricted to one or two<br />

mandatory arguments, depending on whether the defined operator is unary or binary. The<br />

functions return the result of an expression of the form:<br />

[operand] operator operand<br />

Each dummy argument of the functions listed in the interface block must have the<br />

INTENT(IN) attribute. If operator is intrinsic, each specified function must take the same<br />

number of arguments as the intrinsic operator has operands. Furthermore, the arguments<br />

must be distinguishable from those normally associated with the intrinsic operation.<br />

However, argument keywords must not be used when the argument is specified as an operand<br />

to a defined operator.<br />

If a user-defined operator is referenced by its generic name, the reference must resolve to a<br />

unique, specific function name. The selection of the function is accomplished by matching the<br />

number, type, kind, and rank of the operand with the dummy argument lists of the functions<br />

specified in the interface block. As with generic name references (see “Generic procedures” on<br />

page 151), exactly one procedure must match the properties of the operands, and the<br />

matching function is selected and invoked.<br />

The following program, def_op.f90, illustrates a defined operation. The operation, .inrect.,<br />

compares two derived-type operands. The one operand holds the x and y co-ordinates of a<br />

point on a graph, and the other holds the set of co-ordinates defining a rectangle. If the point<br />

is inside the rectangle, the operation evaluates to .TRUE.. The module in which the operation<br />

is defined also contains the definitions of the types of the operands.<br />

As noted in the comments, when a module is defined in the same file as any USE statements<br />

that reference the module, the definition must lexically precede the USE statements. For<br />

information about modules and the USE statement, see “Modules” on page 158.<br />

Example 7-9 def_op.f90<br />

! Note that, if a module definition and any USE statements that<br />

! reference the definition are in the same file, then the<br />

! definition must lexically precede the USE statements.<br />

MODULE coord_op_def<br />

! Defines a logical operation for comparing two derived-type<br />

Chapter 7 153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!