Previous Topic Next topic Print topic


Operators

A set of default operators are predefined and implemented. You can overload an operator and define your own behavior for it.

operator-specification

operator-header procedure-division

operator-header

operator-name attribute-clause

operator-name

binary-operator
comparison-operator arithmetic-operator bitwise-operator
  • =
  • <>
  • <=
  • <
  • >=
  • >
  • +
  • -
  • *
  • /
  • **
  • B-AND
  • B-OR
  • B-XOR
  • B-LEFT
  • B-RIGHT
unary-operator
  • +
  • -
  • B-NOT
  • EXPLICIT
  • IMPLICIT

Example

class-id MyType.
operator-id + 
procedure division using by value o as type MyType, I as binary-long
                           returning ret as type MyType.  
  ...
end operator.
end class.

See also the Operator Overloading sample, available from $COBDIR/demo.

Conversion Operators, IMPLICIT and EXPLICIT

Conversion operators can be overloaded, so that the appropriate conversion operator is used according to the parameter types in the calling code. The operator with the matching signature (procedure division using clause) is executed. For example, where a data item of type Timer requires converting to binary-long, the operator used is the operator with an input parameter of type Timer and output parameter of binary-long.

To define and use conversion operators:

  • Use the IMPLICIT or EXPLICIT keyword on the operator-id header
  • The operator converts the types specified in the operator's signature (its procedure division using clause)
  • The types converted can be either classes or value-types, but not interfaces
  • Types that inherits from one another cannot be converted using a conversion operator
  • The same conversion cannot be defined twice: once as implicit and as explicit

Operator Overloading

You can overload operators to provide alternative behavior, or behavior for different operand types. For example, where a Timer type is expressed as hours and minutes, you could define one + (plus) operator to add two Timers and another + (plus) operator to add a number of minutes to a Timer type.

To declare overloaded operators:

  • Overloaded operators are public and static. They cannot be defined as abstract, final or override.
  • The parameters in the operator's signature must be declared by value. They must have a returning item.
  • The following operators can be overloaded, and require the following operator-id signatures:
Operators that can be overloaded Overload operator-id signature Notes
Conditional operators, such as:

t1 = t2, t1 equals t2

value t1 t2 returning condition-value

Conditional operators must be specified in pairs. If you declare an equality operator (=), you must also declare an inequality operator (<>).

The returning item must be of type condition-value.

Binary operators, such as:

t1 + t2

value t1 t2 returning t3

Binary operators must have two arguments. For binary operators, you might need to specify a pair of operators to enable the two operands to be passed in either order.
Unary operators, such as:

-t1

value t1 returning t3

Unary overloaded operators can have only one argument.
Logical operators, such as:

t1 b-or

value t1 t2 returning t3

 
Shift operators, such as:

t1 b-right

value t1 t2 returning t3

 

While the arithmetic operators for addition, subtraction, multiplication and division can all be overloaded, their verb equivalents cannot. For example, ADD in the following statement cannot be overloaded:

       add a to b giving c 

To use overloaded operators, the overloaded operator must be in scope.

Previous Topic Next topic Print topic