The generic name must be explicitly declared with the GENERIC attribute.
Abbreviation: OTHER for OTHERWISE
For the general declaration syntax, see DECLARE statement.
Structures or unions cannot be specified.
Where a descriptor is not required, its absence must be indicated by an asterisk.
The descriptor that represents the absence of all arguments in the invoking statement is expressed by omitting the generic descriptor in the WHEN clause of the entry. It has the form:
generic (... entry1 when( ) ...)
When an invocation of a generic name is encountered, the number of arguments specified in the invocation and their attributes are compared with descriptor list of each entry in the set. The first entry reference for which the descriptor list matches the arguments both in number and attributes replaces the generic name.
In the following example, an entry reference that has exactly two descriptors with the attributes DECIMAL or FLOAT, and BINARY or FIXED is searched for.
declare Calc generic (
Fxdcal when (fixed,fixed),
Flocal when (float,float),
Mixed when (float,fixed),
Error otherwise);
Dcl X decimal float (6),
Y binary fixed (15,0);
Z = X+Calc(X,Y);
If an entry with the exact number of descriptors with the exact attributes is not found, the entry with the OTHERWISE clause is selected if present. In the previous example, Mixed is selected as the replacement.
In a similar manner, an entry can be selected based on the dimensionality of the arguments.
dcl D generic (D1 when ((*))),
D2 when((*,*))),
A(2),
B(3,5);
call D(A); /* D1 selected because A has one dimension */
call D(B); /* D2 selected because B has two dimensions */
If all of the descriptors are omitted or consist of an asterisk, the first entry reference with the correct number of descriptors is selected.
An entry expression used as an argument in a reference to a generic value matches only a descriptor of type ENTRY. If there is no such description, the program is in error.