Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Defined Operations

When operators are defined for functions, the functions can then be referenced as defined operations.

The operators are defined by using a generic interface block or a GENERIC statement that specifies OPERATOR, followed by the defined operator (in parentheses).

A defined operation is not an intrinsic operation. You can use a defined operation to extend the meaning of an intrinsic operator, but you cannot redefine an intrinsic operator. For example:

  INTERFACE OPERATOR (+)
    INTEGER FUNCTION IADD (I, J)
      INTEGER,INTENT(IN) :: I, J
    END FUNCTION
  END INTERFACE

The above is not permitted because the interface for IADD takes two default integer arguments, and intrinsic addition is already defined for two integers. The following is valid, because the plus operator is not intrinsically defined for CHARACTER operands:

  INTERFACE OPERATOR (+)
     INTEGER FUNCTION IADD (I, J)
       CHARACTER(LEN=*),INTENT(IN) :: I, J
    END FUNCTION
  END INTERFACE

For defined unary operations, the function must contain one argument. For defined binary operations, the function must contain two arguments.

Interpretation of the operation is provided by the function that defines the operation.

A Standard Fortran defined operator can contain up to 31 letters, and is enclosed in periods (.), or it may be an intrinsic operator.

An intrinsic operator can be followed by a defined unary operator.

The result of a defined operation can have any type. The type of the result (and its value) must be specified by the defining function.

Examples

The following examples show expressions containing defined operators:

  .COMPLEMENT. A
  X .PLUS. Y .PLUS. Z
  M * .MINUS. N