NETJVM 

The Operator-ID Paragraph

The Operator-ID paragraph indicates that this Identification Division introduces an operator definition and specifies the symbol or keyword that identifies the operator.

General Format

Operator-ID

Syntax Rules

  1. The Operator-ID paragraph may be specified only within a factory definition.
  2. An implicit conversion should only be used when the target type is able to represent all possible values of the source type.
  3. An explicit conversion, which is specified using casting syntax (object-name AS type-name), can be used when there is a possibility of failure.
  4. If a comparison operator is specified, a boolean data item must be specified in the RETURNING phrase of the Procedure Division header.
  5. Except for unary +, unary –, IMPLICIT and EXPLICIT, there must be two formal parameters specified in the USING phrase of the Procedure Division header, at least one of which must be of the class in which this operator is defined.
  6. If IMPLICIT or EXPLICIT is specified, there must be one formal parameter specified in the USING phrase of the Procedure Division header and one data item specified in the RETURNING phrase of that header. Either the formal parameter or the return value must be of the class in which this operator is defined.
  7. Use the extension keyword to extend the operator.
  8. The operator to extend must be declared in a static class.

General Rules

  1. The new meaning for the operator can be anything, but it is a good idea if it is consistent with the normal understanding of the operator.
  2. Overloaded arithmetic expressions can be specified anywhere that a normal arithmetic expression can be specified.
  3. Implicit and explicit conversions give a way of assigning an item of one type to another type, even when neither type inherits, directly or indirectly, from the other.
  4. You can tell if an operator is overloaded by using a class browser such as Reflector, look for factory (static) methods whose names begin with the prefix op_.

Example

To extend types in managed COBOL, use the following construct:

       class-id MyOperatorExtension static.

       operator-id + extension.
       procedure division using by value c1 as type MyClass,
                                         c2 as type MyClass,
                      returning ret as type MyClass.
           set ret to null *> or something more complex
           goback
       end operator.

       end class.