GENERIC attribute
The generic name must be explicitly declared with the GENERIC attribute.
Abbreviation: OTHER for OTHERWISE
For the general declaration syntax, see DECLARE statement.
- entry-ref
- Must not be subscripted or defined. The same entry reference can appear more than once within a single GENERIC declaration with different lists of descriptors.
- generic-descriptor
- Corresponds to a single
argument. It specifies an attribute that the corresponding argument
must have so that the associated entry reference can be selected for
replacement.
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( ) ...) - data-attributes
- Listed in Data types and attributes.
- ALIGNED and UNALIGNED
- See ALIGNED and UNALIGNED attributes.
- ASSIGNABLE and NONASSIGNABLE
- See ASSIGNABLE and NONASSIGNABLE attributes.
- CONNECTED and NONCONNECTED
- See CONNECTED and NONCONNECTED attributes.
- HEXADEC and IEEE
- See HEXADEC and IEEE attributes.
- BIGENDIAN and LITTLEENDIAN
- See BIGENDIAN and LITTLEENDIAN attributes.
- OPTIONAL
- See OPTIONAL attribute.
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.
