3.8.1 Variant Parts and Discrete Choices
Language Design Principles
The definition of “cover” in this
subclause and the rules about discrete choices are designed so that they
are also appropriate for array aggregates and case statements.
The rules of this subclause intentionally parallel
those for case statements.
Syntax
Name Resolution Rules
Legality Rules
The discriminant of the
variant_part
shall be of a discrete type.
Ramification: It shall not be of an access
type, named or anonymous.
{cover
a value (by a discrete_choice) [partial]} A
discrete_choice
is defined to
cover a value in the following cases:
Ramification: For
case_statements,
this includes values outside the range of the static subtype (if any)
to be covered by the choices. It even includes values outside the base
range of the case expression's type, since values of numeric types (and
undefined values of any scalar type?) can be outside their base range.
The possible values
of the discriminant of a
variant_part
shall be covered as follows:
If the discriminant is of a static constrained
scalar subtype, then each non-
others discrete_choice
shall cover only values in that subtype, and each value of that subtype
shall be covered by some
discrete_choice
[(either explicitly or by
others)];
If the type of the discriminant is a descendant
of a generic formal scalar type then the
variant_part
shall have an
others discrete_choice;
Reason: The base range is not known statically
in this case.
Otherwise, each value of the base range of the
type of the discriminant shall be covered [(either explicitly or by others)].
Static Semantics
Dynamic Semantics
{elaboration (variant_part)
[partial]} The elaboration of a
variant_part
consists of the elaboration of the
component_list
of each
variant
in the order in which they appear.
Examples
Example of record
type with a variant part:
type Device is (Printer, Disk, Drum);
type State is (Open, Closed);
type Peripheral(Unit : Device := Disk) is
record
Status : State;
case Unit is
when Printer =>
Line_Count : Integer range 1 .. Page_Size;
when others =>
Cylinder : Cylinder_Index;
Track : Track_Number;
end case;
end record;
Examples of record
subtypes:
subtype Drum_Unit is Peripheral(Drum);
subtype Disk_Unit is Peripheral(Disk);
Examples of constrained
record variables:
Writer : Peripheral(Unit => Printer);
Archive : Disk_Unit;
Extensions to Ada 83
{
extensions to Ada 83}
In
Ada 83, the discriminant of a
variant_part
is not allowed to be of a generic formal type. This restriction is removed
in Ada 95; an
others discrete_choice
is required in this case.
Wording Changes from Ada 83
We have added the term Discrete Choice to the
title since this is where they are talked about. This is analogous to
the name of the subclause "Index Constraints and Discrete Ranges"
in the clause on Array Types.
The rule requiring that the discriminant denote
a discriminant of the type being defined seems to have been left implicit
in RM83.