3.8.1 Variant Parts and Discrete Choices
1
1.a
Language Design Principles
1.b
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.
1.c
The rules of this subclause intentionally parallel
those for case statements.
Syntax
2
3
4
5/3
Name Resolution Rules
6
6.a
Legality Rules
7
The discriminant of the
variant_part
shall be of a discrete type.
7.a
Ramification: It shall not be of an access
type, named or anonymous.
8/3
9
10/3
10.1/3
10.a/3
11/3
12
12.a
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.
13
14
The possible values
of the discriminant of a
variant_part
shall be covered as follows:
15/3
{
AI05-0153-3}
{
AI05-0188-1}
{
AI05-0262-1}
If the discriminant is of a static constrained scalar subtype
,
then
, except within an instance of a generic unit,
each non-
others discrete_choice
shall cover only values in that subtype
that satisfy
its predicate, and each value of that subtype
that
satisfies its predicate shall be covered by some
discrete_choice
[(either explicitly or by
others)];
15.a/3
Reason:
{
AI05-0188-1}
The exemption for a discriminated type declared
in an instance allows the following example:
15.b/3
generic
type T is new Integer;
package G is
type Rec (Discrim : T) is record
case Discrim is
when -10 .. -1 =>
Foo : Float;
when others =>
null;
end case;
end record;
end G;
15.c/3
package I is new G (Natural); -- Legal
16/3
16.a
Reason: The base range is not known statically
in this case.
17
Otherwise, each value of the base range of the
type of the discriminant shall be covered [(either explicitly or by others)].
18
Static Semantics
19
20
Dynamic Semantics
21
22
Examples
23
Example of record
type with a variant part:
24
type Device is (Printer, Disk, Drum);
type State is (Open, Closed);
25
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;
26
Examples of record
subtypes:
27
subtype Drum_Unit is Peripheral(Drum);
subtype Disk_Unit is Peripheral(Disk);
28
Examples of constrained
record variables:
29
Writer : Peripheral(Unit => Printer);
Archive : Disk_Unit;
Extensions to Ada 83
29.a
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
29.b
29.c
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.
29.d
The rule requiring that the discriminant denote
a discriminant of the type being defined seems to have been left implicit
in RM83.
Incompatibilities With Ada 2005
29.e/3
{
AI05-0158-1}
Membership tests are no longer
allowed as a discrete_choice,
in order that those tests can be expanded to allow multiple tests in
a single expression without ambiguity. Since a membership test has a
boolean type, they are very unlikely to be used as a discrete_choice.
Extensions to Ada 2005
29.f/3
{
AI05-0153-3}
Subtypes with static predicates
can be used in discrete_choices,
and the coverage rules are modified to respect the predicates.
29.g/3
{
AI05-0188-1}
Variants in generic specifications are no longer
rejected if the subtype of the actual type does not include all of the
case choices. This probably isn't useful, but it is consistent with the
treatment of case_expressions.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe