To: J3 J3/19-142 From: Malcolm Cohen Subject: TYPEOF and CLASSOF Date: 2019-February-12 1. Introduction Whichever scheme is preferred for generic programming, there are many cases where it is desired to declare something (usually a temporary) that has the same type and type parameters as another object. This is even the case if generic programming ends up being left to the already-existing technologies at this time, such as INCLUDE or fpp/cpp. This paper describes the common use-case and proposes a standalone feature. Only the type and type parameters are being considered at this time, not rank or shape (some future paper should perhaps address those). 2. A simple use case A generic inline swap, whether using existing technology or some other. An fpp macro to do this could look something like the following: #define SWAP(A,B) \ Associate(x=>A,y=>B); \ ! Establish associations to avoid naming issues Block; \ TYPEOF(x) temp; \ temp = x; \ x = y; \ y = temp; \ End Block; \ End Associate In the case of swapping the value of allocatable polymorphic variables, perhaps something like the following would be suitable: #define PASWAP(A,B) \ Associate(x=>A,y=>B); \ ! Establish associations to avoid naming issues Block; \ CLASSOF(x),ALLOCATABLE :: temp; \ temp = x; \ x = y; \ y = temp; \ End Block; \ End Associate This would be useful even when the technology being used is INCLUDE files; one would simply put the ASSOCIATE-ENDASSOCIATE around the INCLUDE line. 3. Proposed formal requirements - that there be some mechanism for declaring the type and type parameters of an entity based on the type and type parameters of another entity; - that this be capable of handling polymorphic entities; - that other attributes (such as SAVE, ALLOCATABLE, or DIMENSION) not be part of this feature. 4. Proposed formal specifications - that there be a statement which declares one or more entities to have the type and type parameters of another single entity; - the single entity can be any data object including constants, but the entities being declared can be procedures, data objects, or components; - for convenience, that statement should be able to specify additional attributes such as SAVE and VOLATILE; - that a separate form be provided for declaring polymorphic entities, which may be dummy arguments, allocatables, or pointers; - the polymorphic form is not allowed for objects of intrinsic type; - if a length type parameter of the single entity is deferred, then it is also deferred in the entities being declared (and that means that they must have the ALLOCATABLE or POINTER attribute); - if a length type parameter of the single entity is not deferred, then the value in the entities being declared is the same value as the single entity. 5. Proposed syntax In a type declaration statement or a component definition statement, the type-spec may be replaced by a typeof-spec or classof-spec. typeof-spec <> TYPEOF ( data-ref ) classof-spec <> CLASSOF ( data-ref ) In the case of TYPEOF, data-ref may be intrinsic type, derived type, or polymorphic but not unlimited polymorphic. It always declares nonpolymorphic entities whose declared type and type parameters are the same as data-ref. In the case of CLASSOF, data-ref need not be polymorphic, but shall not be of intrinsic type. It may be unlimited polymorphic. It always declares polymorphic entities whose declared type and type parmeters are the same as data-ref (and deferred if those of data-ref are deferred). Nothing is said about the dynamic type, which will follow the usual rules for a polymorphic entity. ===END===