Polymorphic entities (Fortran 2003)

A polymorphic entity is a data entity that is able to be of differing types during program execution. The type of a data entity at a particular point during execution of a program is its dynamic type. The declared type of a data entity is the type that it is declared to have, either explicitly or implicitly.

You use the CLASS type specifier to declare polymorphic objects. If the CLASS type specifier contains a type name, the declared type of a polymorphic object is that type.

You can use the CLASS(*) specifier to declare an unlimited polymorphic object. An unlimited polymorphic entity is not declared to have a type. It is not considered to have the same declared type as any other entity, including another unlimited polymorphic entity.

A nonpolymorphic entity is type-compatible only with entities of the same type. For a polymorphic entity, type compatibility is based on its declared type: a polymorphic entity that is not unlimited polymorphic is type-compatible with entities of the same type or any of its extensions. Even though an unlimited polymorphic entity is not considered to have a declared type, it is type-compatible with all entities.

An entity is said to be type-compatible with a type if it is type-compatible with entities of that type. An entity is type-, kind-, and rank-compatible (TKR) with another entity if the first entity is type-compatible with the second, the kind type parameters of the first entity have the same values as corresponding kind type parameters of the second, and both entities have the same rankTS begins or either is assumed-rankTS ends.

Only components of the declared type of a polymorphic object may be designated by component selection.

A polymorphic allocatable object may be allocated to be of any type with which it is type-compatible. A polymorphic pointer or dummy argument may, during program execution, be associated with objects with which it is type-compatible.

The following table lists the dynamic type of objects.
Table 1. Dynamic type of objects
Object Dynamic type
Allocated allocatable polymorphic object The type with which the object was allocated.
Associated polymorphic pointer The dynamic type of the pointer's target.
Nonallocatable nonpointer polymorphic dummy argument The dynamic type of dummy's associated actual argument.
Deallocated allocatable The allocatable object's declared type.
Disassociated pointer The pointer's declared type.
Entity identified by an associate name The dynamic type of the selector with which the object is associated.
Nonpolymorphic object The object's declared type.

Related information