TYPE

Purpose

A TYPE type declaration statement specifies the type, type parameters, and attributes of objects and functions of derived type. Initial values can be assigned to objects.

Fortran 2008 beginsThe TYPE type declaration statement can declare entities of both derived type and intrinsic type.Fortran 2008 ends

Syntax

Read syntax diagramSkip visual syntax diagramTYPE( intrinsic_type_spec1derived_type_spec ) ::,attr_spec_list:: entity_decl_list
Notes:
  • 1 Fortran 2008
where:
Fortran 2008 beginsintrinsic_type_specFortran 2008 ends
is the name of an intrinsic data type. For more information, see Intrinsic data types.
derived_type_spec
is the name of an extensible derived type. For more information, see Type Declaration.

Fortran 2003 begins The derived type must not be abstract. Fortran 2003 ends

::
is the double colon separator. It is required if attributes are specified, = constant_expr is used, or =>NULL() appears as part of any entity_decl.
attr_spec
is any of the following attributes. For detailed information on rules about a particular attribute, refer to the statement of the same name.
ALLOCATABLE  1  INTRINSIC PUBLIC
ASYNCHRONOUS OPTIONAL SAVE
AUTOMATIC  2  PARAMETER STATIC  2 
BIND  1  POINTER TARGET
DIMENSION (array_spec) PRIVATE VALUE  1 
EXTERNAL PROTECTED  1  VOLATILE
INTENT (intent_spec)    
Note:
  •  1  Fortran 2003
  •  2  IBM extension
where:
array_spec
is a list of dimension bounds.
intent_spec
is one of IN, OUT, or INOUT.
entity_decl
Read syntax diagramSkip visual syntax diagrama (array_spec) /initial_value_list/1 = constant_expr => NULL()
Notes:
  • 1 IBM extension.
where:
a
is an object name or function name. array_spec cannot be specified for a function with an implicit interface.
IBM extension begins initial_value IBM extension ends
provides an initial value for the entity specified by the immediately preceding name. Initialization occurs as described in DATA.
constant_expr
provides a constant expression for the entity specified by the immediately preceding name.
=> NULL()
provides the initial value for a pointer object.

Rules

Within the context of a derived type definition:
  • If => appears in a component initialization, the POINTER attribute must appear in the attr_spec_list.
  • If = appears in a component initialization, the POINTER attribute cannot appear in the component attr_spec_list.
  • The compiler will evaluate constant_expr within the scoping unit of the type definition.

If => appears for a variable, the object must have the POINTER attribute.

If constant_expr appears for a variable, the object cannot have the POINTER attribute.

Entities in type declaration statements are constrained by the rules of any attributes specified for the entities, as detailed in the corresponding attribute statements.

Once a derived type has been defined, you can use it to define your data items using the TYPE type declaration statement. When an entity is explicitly declared to be of a derived type, that derived type must have been previously defined in the scoping unit or is accessible by use or host association.

The data object becomes an object of derived type or a structure. Each structure component is a subobject of the object of derived type.

If you specify the DIMENSION attribute, you are creating an array whose elements have a data type of that derived type.

Other than in specification statements, you can use objects of derived type as actual and dummy arguments, and they can also appear as items in input/output lists (unless the object has a component with the POINTER attribute), assignment statements, structure constructors, and the right side of a statement function definition. If a structure component is not accessible, a derived-type object cannot be used in an input/output list or as a structure constructor.

Objects of nonsequence derived type cannot be used as data items in EQUIVALENCE and COMMON statements. Objects of nonsequence data types cannot be integer pointees.

A nonsequence derived-type dummy argument must specify a derived type that is accessible through use or host association to ensure that the same derived-type definition defines both the actual and dummy arguments.

The type declaration statement overrides the implicit type rules in effect.

An object cannot be initialized in a type declaration statement if it is a dummy argument, allocatable object, function result, object in a blank common block, integer pointer, external name, intrinsic name, or automatic object. Nor can an object be initialized if it has the AUTOMATIC attribute. The object may be initialized if it appears in a named common block in a block data program unit IBM extension beginsor if it appears in a named common block in a module or submoduleIBM extension ends.

In Fortran 95, a pointer can be initialized. Pointers can only be initialized by the use of => NULL().

The specification expression of an array_spec can be a nonconstant expression if the specification expression appears in an interface body or in the specification part of a subprogram. Any object being declared that uses this nonconstant expression and is not a dummy argument or a pointee is called an automatic object.

An attribute cannot be repeated in a given type declaration statement, nor can an entity be explicitly given the same attribute more than once in a scoping unit.

constant_expr must be specified if the statement contains the PARAMETER attribute. If the entity you are declaring is a variable, and constant_expr or NULL() is specified, the variable is initially defined.

If the entity you are declaring is a derived type component, and constant_expr or NULL() is specified, the derived type has default initialization.

a becomes defined with the value determined by constant_expr, in accordance with the rules for intrinsic assignment. If the entity is an array, its shape must be specified either in the type declaration statement or in a previous specification statement in the same scoping unit. A variable or variable subobject cannot be initialized more than once. If a is a variable, the presence of constant_expr or NULL() implies that a is a saved object, except for an object in a named common block. The initialization of an object could affect the fundamental storage class of an object.

An array_spec specified in the entity_decl takes precedence over the array_spec in the DIMENSION attribute.

An array function result that does not have the ALLOCTABLE or POINTER attribute must have an explicit-shape array specification.

If the entity declared is a function, it must not have an accessible explicit interface unless it is an intrinsic function. The derived type can be specified on the FUNCTION statement, provided the derived type is defined within the body of the function or is accessible via host or use association.

IBM extension begins If T or F, defined previously as the name of a constant, appears in a type declaration statement, it is no longer an abbreviated logical constant but the name of the named constant. IBM extension ends

Example 1

The following code defines a derived type people using the TYPE type declaration statement.
TYPE people                      
  INTEGER age
  CHARACTER*20 name
END TYPE people
The following statement declares an entity named smith of the derived type people:
TYPE(people) :: smith = people(25,'John Smith')
Fortran 2008 begins

Example 2

This example demonstrates the usage of the TYPE() type specifier to declare entities of intrinsic type.
TYPE(INTEGER) :: i
TYPE(INTEGER(KIND=2)) :: i2
TYPE(INTEGER(4)) :: i4

TYPE(CHARACTER(*)) :: cstar
TYPE(CHARACTER*2) :: c2
TYPE(CHARACTER(LEN=4,KIND=1)) :: c4
TYPE(CHARACTER(7)) :: c7

TYPE derived(l)
  TYPE(INTEGER), LEN :: l
  TYPE(CHARACTER*l) :: cl
  TYPE(COMPLEX), DIMENSION (l) :: cp
END TYPE derived
Fortran 2008 ends

Related information