To: J3 J3/23-215r1 From: Tom Clune Subject: polymorphic deferred types Date: 2023-October-18 Reference: 23-155r2 1. Introduction =============== The current draft syntax for generics features in F202Y can support all of the proffered use cases, except those that involve polymorphism for the deferred type. This paper summarizes the rationale by generics subgroup for how to support this capability in F202Y. 2. Extensible deferred type use cases ===================================== Consider a List as a prototypical category of container. A programmer may wish to declare a list of reals, a list of integers, or a list of some user-defined type. As shown in previous tutorials, the existing syntax can adequately support all of these. But another important use case is where all of the elements of the list have a declared type T, but differing dynamic types. (Here, T is a deferred type argument for a template.) In practice, such a user could leverage a (monomorphic) implementation of the List template by defining a wrapper type: TYPE :: Wrapper CLASS(MyBaseType), ALLOCATABLE :: item END TYPE And then use Wrapper as the argument to the List template. In practice is this is very cumbersome as such a user must wrap or unwrap arguments to each procedure involving List. It would be better to allow the template itself to handle these details. 3. Options for extensible deferred type ====================================== Subgroup considered the following options for allowing a deferred type T to be used in a polymorphic context in a template. (1) Implicitly interpret the template as allowing deferred type T to be used in polymorphic contexts if the template has CLASS(T) anywhere in its definition. (2) Introduce an _intrinsic_ requirement that indicates that T must be an extensible type. This wouldf be a requirement that cannot be specified by existing syntax. Provisional syntax would be: REQUIRES EXTENSIBLE(T) (3) Introduce variant syntax for indicating that a deferred type can be used in polymorphic contexts. polymorphic. Provisional syntax: CLASS, DEFERRED :: T Subgroup did not like option (1) as it could be relatively difficult for the casual programmer to see whether a deferred type is polymorphic. Some obscure usage deep in the source could trigger the difference. Subgroup decided that option (2) would be attractive if related intrinsic requirements were also to be introduced. E.g., REQUIRES TYPE_EXTENDS(T, U) ! different than intrinsic EXTENDS_TYPE() REQUIRES DIFFERENT_TYPE(T,U) ! allows overload of T & U While subgroup believes that such intrinsic requirements may prove desirable in the future, our existing use cases do not clearly show the need at this time. Subgroup therefore prefers the more direct approach with option (3). Our suggested syntax would be: CLASS, DEFERRED :: T 4. Implications and consequences ================================ If a template declares a deferred type T as polymorphic, then any corresponding actual argument shall be an extensible type. I.e., the actual type shall not be: - intrinsic - SEQUENCE - BIND(C) - etc. ===END===