Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

GENERIC

Statement: Declares a generic interface that is bound to a derived type-bound procedure, or specifies a generic identifier for one or more specific procedures.

The GENERIC statement takes the following form:

GENERIC [, access-spec]:: generic-spec => binding-name1 [, binding-name2]...

access-spec

(Optional) Is the PUBLIC or PRIVATE attribute. Only one can be specified. access-spec specifies the accessibility of generic-spec. access-spec can only appear in the specification part of a module.

generic-spec

Is an explicit INTERFACE statement using one of the following:

The same generic-spec can be used in several generic bindings. In this case, every occurrence of the same generic-spec must have the same accessibility.

binding-spec1 [, binding-spec2,...]

Is the name of a specific procedure. It cannot be the name of a specific procedure that has been specified in an accessible generic interface with the same generic identifier. At most one of the specific names can be the same as the generic name.

Examples

Consider the following:

GENERIC :: GEN_FUNC => INT_FUNC, REAL_FUNC
GENERIC :: GEN_FUNC => CMPLX_FUNC 

If INT_FUNC, REAL_FUNC, and CMPLX_FUNC are all functions with accessible interfaces, the first GENERIC statement declares GEN_FUNC to be a generic function name that can resolve to INT_FUNC or REAL_FUNC. The second GENERIC statement extends GEN_FUNC to be resolvable to a third specific function, CMPLX_FUNC.

Consider the following:

TYPE MY_TYPE2
...   
CONTAINS
  PROCEDURE :: MYPROC => MYPROC1
  PROCEDURE,PASS(C) :: UPROC => UPROC1
  GENERIC :: OPERATOR(+) => MYPROC, UPROC
END TYPE MY_TYPE2
...
TYPE,EXTENDS(MY_TYPE2) :: MY_TYPE3
...
CONTAINS
  PROCEDURE :: MYPROC => MYPROC2
  PROCEDURE,PASS(C) :: UPROC => UPROC2
END TYPE MY_TYPE3

The type MY_TYPE3 inherits the generic operator '+'. Invoking the generic (+) invokes the specific type-bound procedure. For entities of type MY_TYPE3, that invokes the overriding actual procedure (MYPROC2 or UPROC2).

Consider the following:

TYPE MY_TYPE4
...   
CONTAINS
  PROCEDURE :: FMTREAD   => MY_TYPE4_FMTREAD
  PROCEDURE :: FMTWRITE  => MY_TYPE4_FMTWRITE
  GENERIC   :: READ  (FORMATTED) => FMTREAD
  GENERIC   :: WRITE (FORMATTED) => FMTWRITE
END TYPE MY_TYPE4

When an object of type MY_TYPE4 appears in an I/O list and is associated with a DT format edit descriptor in a formatted READ statement, MY_TYPE4_FMTREAD is called to perform the read of the object. In a formatted WRITE, MY_TYPE4_FMTWRITE is called to perform the output of the object.