3.4. Derived Types

[PREVIOUS][UP][NEXT]

A derived type definition defines a new (base) type whose characteristics are derived from those of a parent type; the new type is called a derived type. A derived type definition further defines a derived subtype, which is a subtype of the derived type.

    derived_type_definition ::= new subtype_indication 

The subtype indication that occurs after the reserved word new defines the parent subtype. The parent type is the base type of the parent subtype. If a constraint exists for the parent subtype, a similar constraint exists for the derived subtype; the only difference is that for a range constraint, and likewise for a floating or fixed point constraint that includes a range constraint, the value of each bound is replaced by the corresponding value of the derived type. The characteristics of the derived type are defined as follows:

Each operation of the derived type is implicitly declared at the place of the derived type declaration. The implicit declarations of any derived subprograms occur last.

The specification of a derived subprogram is obtained implicitly by systematic replacement of the parent type by the derived type in the specification of the derivable subprogram. Any subtype of the parent type is likewise replaced by a subtype of the derived type with a similar constraint (as for the transformation of a constraint of the parent subtype into the corresponding constraint of the derived subtype). Finally, any expression of the parent type is made to be the operand of a type conversion that yields a result of the derived type.

Calling a derived subprogram is equivalent to calling the corresponding subprogram of the parent type, in which each actual parameter that is of the derived type is replaced by a type conversion of this actual parameter to the parent type (this means that a conversion to the parent type happens before the call for the modes in and in out; a reverse conversion to the derived type happens after the call for the modes in out and out, see 6.4.1). In addition, if the result of a called function is of the parent type, this result is converted to the derived type.

If a derived or private type is declared immediately within the visible part of a package, then, within this visible part, this type must not be used as the parent type of a derived type definition. (For private types, see also section 7.4.1.)

For the elaboration of a derived type definition, the subtype indication is first elaborated, the derived type is then created, and finally, the derived subtype is created.

Examples:

    type LOCAL_COORDINATE is new COORDINATE;   --  two different types
    type MIDWEEK is new DAY range TUE .. THU;  --  see 3.5.1
    type COUNTER is new POSITIVE;              --  same range as POSITIVE 

    type SPECIAL_KEY is new KEY_MANAGER.KEY;   --  see 7.4.2
    -- the derived subprograms have the following specifications: 

    -- procedure GET_KEY(K : out SPECIAL_KEY);
    -- function "<"(X,Y : SPECIAL_KEY) return BOOLEAN;                                              

Notes:

The rules of derivation of basic operations and enumeration literals imply that the notation for any literal or aggregate of the derived type is the same as for the parent type; such literals and aggregates are said to be overloaded. Similarly, it follows that the notation for denoting a component, a discriminant, an entry, a slice, or an attribute is the same for the derived type as for the parent type.

Hiding of a derived subprogram is allowed even within the same declarative region (see 8.3). A derived subprogram hides a predefined operator that has the same parameter and result type profile (see 6.6).

A generic subprogram declaration is not derivable since it declares a generic unit rather than a subprogram. On the other hand, an instantiation of a generic subprogram is a (nongeneric) subprogram, which is derivable if it satisfies the requirements for derivability of subprograms.

If the parent type is a boolean type, the predefined relational operators of the derived type deliver a result of the predefined type BOOLEAN (see 4.5.2).

If a representation clause is given for the parent type but appears after the derived type declaration, then no corresponding representation clause applies to the derived type; hence an explicit representation clause for such a derived type is allowed.

For a derived subprogram, if a parameter belongs to the derived type, the subtype of this parameter need not have any value in common with the derived subtype.

References: access value, actual parameter, aggregate, attribute, base type, basic operation, boolean type, bound of a range, class of type, collection, component, composite type, constraint, conversion, declaration, declarative region, default expression, default initial value for an access type, discriminant, elaboration, entry, enumeration literal, floating point constraint, fixed point constraint, formal parameter, function call, generic declaration, immediately within, implicit declaration, literal, mode, overloading, and 8.7, package, package specification, parameter association, predefined operator, private type, procedure, procedure call statement, range constraint, representation clause, reserved word, slice, subprogram, subprogram specification, subtype indication, subtype, type, type definition, visible part.


[INDEX][CONTENTS]