To: J3 J3/23-102 From: T. Clune & subgroup generics Subject: Formal syntax for generics Date: 2023-January-02 Reference: 22-120r5, 22-151r3, 22-154r5 1. Introduction =============== This paper contains the syntax for generic programming features. Section 2 contains a succinct, non-pedagogical example that exercises most of the proposed syntax. Section 3 provides the formal syntax and is further broken into the following subsections: 3.1 Syntax for the REQUIREMENT construct 3.2 Syntax for the REQUIRES statement 3.3 Syntax for the TEMPLATE construct 3.4 syntax for the INSTANTIATE statement 2. Examples =========== MODULE A REQUIREMENT R(T,F) TYPE, DEFERRED :: T FUNCTION F(x) RESULT(y) TYPE(T) :: y TYPE(T), INTENT(IN) :: x END FUNCTION END REQUIREMENT TEMPLATE B(T,F,C) REQUIRES R(T,F) ! provides interface for F TYPE, DEFERRED :: T ! redundant INTEGER, CONSTANT :: C(..) ! deferred rank TYPE(T) :: var END TEMPLATE B END MODULE A MODULE B USE MODULE A TYPE :: MY_TYPE REAL :: val END TYPE INSTANTIATE B(MY_TYPE, SQUARE, [3,4]), ONLY: my_var => var CONTAINS FUNCTION SQUARE(x) RESULT(y) TYPE(MY_TYPE) :: y TYPE(MY_TYPE), INTENT(IN) :: x y%val = x%val ** 2 END FUNCTION SUBROUTINE DO_SOMETHING() my_var%val = my_var%val + 1. END SUBROUTINE END MODULE B 3. Formal Syntax ================ 3.1 Syntax for the REQUIREMENT construct ---------------------------------------- <> REQUIREMENT ( [] ) [ ] ... ... END [REQUIREMENT []] Note: is defined in section 3.1.1, and requirement declaration statements are define in section 3.1.2. <> Constraint: Each shall be one of , , or be specified in one of , or , within . Note: If a appears in the it can only be used to define a . Constraint: A declaration shall not have an nor shall it appear in a PUBLIC statement. Note: Requirement dummy arguments are local identifiers and are not externally accessible. <> <> <> <> 3.1.1 Requirement interface body -------------------------------- <> [ ] <> [ ] <> [ ] ... [ ] ... <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> [ [ , ] ... :: ] <> ALLOCATABLE <> ASYNCHRONOUS <> CODIMENSION <> CONTIGUOUS <> DIMENSION ( ) <> INTENT ( ) <> OPTIONAL <> PARAMETER <> POINTER <> <> TARGET <> VALUE <> VOLATILE 3.1.2 Requirement const and type declarations --------------------------------------------- <> , :: <> [,]... CONSTANT [,]... <> CONTIGUOUS <> <> Constraint: An entity declared in shall be INTEGER, LOGICAL, or assumed-length character. <> Constraint: Each shall appear in a . <> TYPE, DEFERRED :: <> Constraint: Each shall appear in . 3.2 Syntax for the REQUIRES statement ------------------------------------- <> REQUIRES [::] ( [] ) Constraint: must be previously defined by a . <> [ keyword = ] Constraint: Each keyword shall be the name of a in the referenced requirement. 3.3 Syntax for the TEMPLATE construct -------------------------------------