Generic binding

Syntax of a generic_binding

The form of generic_binding is:

Read syntax diagramSkip visual syntax diagramGENERIC ,PRIVATE,PUBLIC ::generic_spec=>binding_name_list
The generic_spec can be any of the following:
generic_name
OPERATOR(defined-operator)
The interface of each binding must be as specified in Defined operators.
ASSIGNMENT(=)
The interface of each binding must be as specified in Defined assignment.
dtio_generic_spec
The interface of each binding must be as specified in User-defined derived-type Input/Output procedure interfaces (Fortran 2003).

If the generic_spec is a generic_name, the generic_name cannot be the name of a nongeneric binding of the type. The same generic_spec may be used in several generic bindings within a single derived-type definition. In this case, every occurrence of the same generic_spec must have the same accessibility. Each binding name in the binding_name_list must be the name of a specific binding of the type.

When generic_spec is not a generic_name, each specific binding name in the binding_name_list must have the passed-object dummy argument. You can only specify one binding attribute, PRIVATE or PUBLIC. The following is an example of a generic binding where generic_spec is ASSIGNMENT(=).

Examples

! See example of a procedure with a specific binding for definition of COLOR_POINT
TYPE, EXTENDS(color_point) :: point_info
! An extension of TYPE(COLOR_POINT)

   REAL :: color_code
   CONTAINS
   PROCEDURE, NOPASS:: get_color_code
   PROCEDURE :: info1 => color_to_info
   PROCEDURE :: point1 => point_to_info
   GENERIC :: ASSIGNMENT(=) => info1, point1
END TYPE point_info

CONTAINS
ELEMENTAL SUBROUTINE color_to_info(a, b)
   CLASS(point_info), INTENT(OUT) :: a
   TYPE(color_point), INTENT(IN):: b
   a%color_point = b
   a%color_code = get_color_code(b%color)
END SUBROUTINE
ELEMENTAL SUBROUTINE point_to_info(a, b)
   CLASS(point_info), INTENT(OUT) :: a
   TYPE(point), INTENT(IN):: b
   a%color_point = color_point(point=b, color=1)
   a%color_code = get_color_code(1)
END SUBROUTINE

The following is an example of type parameters that illustrates how length parameters should be used. As illustrated in the example, multiple procedures must be defined for multiple kind parameter values:
! Separate specific bindings may be needed for multiple possible kind parameters:
TYPE :: GRAPH (PREC,NNODES)
  INTEGER, KIND :: PREC
  INTEGER, LEN  :: NNODES
  REAL(PREC) :: XVAL(NNODES), YVAL(NNODES)
CONTAINS
  PROCEDURE, PASS :: FINDMAX_Y_4
  PROCEDURE, PASS :: FINDMAX_Y_8
  GENERIC :: FINDMAX_Y => FINDMAX_Y_4, FINDMAX_Y_8
END TYPE GRAPH

CONTAINS
INTEGER FUNCTION FINDMAX_Y_4(G)
  CLASS(GRAPH(4,*)) :: G
  FINDMAX_Y_4 = MAXLOC(G%XVAL,1)
END FUNCTION FINDMAX_Y_4

INTEGER FUNCTION FINDMAX_Y_8(G)
  CLASS(GRAPH(8,*)) :: G
  FINDMAX_Y_8 = MAXLOC(G%XVAL,1)
END FUNCTION FINDMAX_Y_8