Concepts of Programming Languages

(Sean Pound) #1
6.14 Type Equivalence 307

However, variables of type Derived_Small_Int are not compatible with any
Integer type. On the other hand, variables of type Subrange_Small_Int
are compatible with variables and constants of Integer type and any subtype
of Integer.
For variables of an Ada unconstrained array type, structure type equiva-
lence is used. For example, consider the following type declaration and two
object declarations:


type Vector is array (Integer range <>) of Integer;
Vector_1: Vector (1..10);
Vector_2: Vector (11..20);


The types of these two objects are equivalent, even though they have differ-
ent names and different subscript ranges, because for objects of unconstrained
array types, structure type equivalence rather than name type equivalence is
used. Because both types have 10 elements and the elements of both are of type
Integer, they are type equivalent.
For constrained anonymous types, Ada uses a highly restrictive form of
name type equivalence. Consider the following Ada declarations of constrained
anonymous types:


A : array (1..10) of Integer;


In this case, A has an anonymous but unique type assigned by the compiler and
unavailable to the program. If we also had


B : array (1..10) of Integer;


A and B would be of anonymous but distinct and not equivalent types, though
they are structurally identical. The multiple declaration


C, D : array (1..10) of Integer;


creates two anonymous types, one for C and one for D, which are not equivalent.
This declaration is actually treated as if it were the following two declarations:


C : array (1..10) of Integer;
D : array (1..10) of Integer;


Note that Ada’s form of name type equivalence is more restrictive than the
name type equivalence that is defined at the beginning of this section. If we
had written instead


type List_10 is array (1..10) of Integer;
C, D : List_10;


then the types of C and D would be equivalent.

Free download pdf