[Home] [Prev] [Next] [Index]

3.4 Derived Types

3.4 Derived Types

1
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.

2
derived_type_definition ::= new subtype_indication

3
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:

4 ·
The derived type belongs to the same class of types as the parent type. The set of possible values for the derived type is a copy of the set of possible values for the parent type. If the parent type is composite, then the same components exist for the derived type, and the subtype of corresponding components is the same.

5 ·
For each basic operation of the parent type, there is a corresponding basic operation of the derived type. Explicit type conversion of a value of the parent type into the corresponding value of the derived type is allowed and vice versa as explained in section 4.6.

6 ·
For each enumeration literal or predefined operator of the parent type there is a corresponding operation for the derived type.

7 ·
If the parent type is a task type, then for each entry of the parent type there is a corresponding entry for the derived type.

8 ·
If a default expression exists for a component of an object having the parent type, then the same default expression is used for the corresponding component of an object having the derived type.

9 ·
If the parent type is an access type, then the parent and the derived type share the same collection; there is a null access value for the derived type and it is the default initial value of that type.

10 ·
If an explicit representation clause exists for the parent type and if this clause appears before the derived type definition, then there is a corresponding representation clause (an implicit one) for the derived type.

11 ·
Certain subprograms that are operations of the parent type are said to be derivable. For each derivable subprogram of the parent type, there is a corresponding derived subprogram for the derived type. Two kinds of derivable subprograms exist. First, if the parent type is declared immediately within the visible part of a package, then a subprogram that is itself explicitly declared immediately within the visible part becomes derivable after the end of the visible part, if it is an operation of the parent type. (The explicit declaration is by a subprogram declaration, a renaming declaration, or a generic instantiation.) Second, if the parent type is itself a derived type, then any subprogram that has been derived by this parent type is further derivable, unless the parent type is declared in the visible part of a package and the derived subprogram is hidden by a derivable subprogram of the first kind.

12
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.

13
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.

14
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.

15
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.)

16
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.

17
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:

18
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.

19
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).

20
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.

21
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).

22
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.

23
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.

24
References:

*
access value 3.8

*
actual parameter 6.4.1

*
aggregate 4.3

*
attribute 4.1.4

*
base type 3.3

*
basic operation 3.3.3

*
boolean type 3.5.3

*
bound of a range 3.5

*
class of type 3.3

*
collection 3.8

*
component 3.3

*
composite type 3.3

*
constraint 3.3

*
conversion 4.6

*
declaration 3.1

*
declarative region 8.1

*
default expression 3.2.1

*
default initial value for an access type 3.8

*
discriminant 3.3

*
elaboration 3.9

*
entry 9.5

*
enumeration literal 3.5.1

*
floating point constraint 3.5.7

*
fixed point constraint 3.5.9

*
formal parameter 6.1

*
function call 6.4

*
generic declaration 12.1

*
immediately within 8.1

*
implicit declaration 3.1

*
literal 4.2

*
mode 6.1

*
overloading 6.6

*
overloading 8.7

*
package 7

*
package specification 7.1

*
parameter association 6.4

*
predefined operator 4.5

*
private type 7.4

*
procedure 6

*
procedure call statement 6.4

*
range constraint 3.5

*
representation clause 13.1

*
reserved word 2.9

*
slice 4.1.2

*
subprogram 6

*
subprogram specification 6.1

*
subtype indication 3.3.2

*
subtype 3.3

*
type 3.3

*
type definition 3.3.1

*
visible part 7.2



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-2000, Rational Software Corporation. All rights reserved.