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
Syntax Rules
- The Operator-ID paragraph may be specified only within a factory definition.
- An implicit conversion should only be used when the target type is able to represent all possible values of the source type.
- An explicit conversion, which is specified using casting syntax (object-name AS type-name), can be used when there is a possibility
of failure.
- If a comparison operator is specified, a boolean data item must be specified in the RETURNING phrase of the Procedure Division
header.
- 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.
- 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.
- Use the
extension keyword to extend the operator.
- The operator to extend must be declared in a static class.
General Rules
- 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.
- Overloaded arithmetic expressions can be specified anywhere that a normal arithmetic expression can be specified.
- 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.
- 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
JVM 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.