Rationale for Ada 2012
3.7 Qualified expressions
We conclude this discussion of expressions by considering
some points regarding
names and
primarys.
In Ada 2005 we have
name ::=
direct_name | explicit_dereference | indexed_component
| slice | selected_component | attribute_reference
| type_conversion | function_call | character_literal
primary ::=
numeric_literal | null | string_literal | aggregate | name
| qualified_expression | allocator | (expression)]
And in Ada 2012 we
have
name ::=
direct_name | explicit_dereference | indexed_component
| slice | selected_component | attribute_reference
| type_conversion | function_call | character_literal
| qualified_expression | generalized_reference
| generalized_indexing
primary ::=
numeric_literal | null | string_literal | aggregate | name
| allocator | (expression)]
| (conditional_expression) | (quantified_expression)
The important thing to observe here is that qualified_expression
has moved from being a form of primary to
being a name.
We also note the addition of
conditional_expression
and
quantified_expression (both in parentheses)
as forms of
primary as discussed earlier in
this chapter and the addition of
generalized_reference
and
generalized_indexing as forms of name.
These are used in the new forms of
iterator
briefly alluded to at the end of the discussion on quantified expressions
and which will be discussed in Section
6.3.
Returning to qualified expressions, the main reason
for allowing them as
names is to avoid unnecessary
conversions as mentioned in the Introduction (see
1.3.2).
Consider
A: T; -- object of type T
type Art is array (1 .. 10) of T; -- array of type T
function F(X: Integer) return Art;
A function call can
be used as a prefix and so a call returning
an array can be indexed as in
A := F(3)(7);
which assigns to A the
value of the 7th component of the array returned by the call of F.
Now suppose that F
is overloaded so that F(3) is ambiguous. The
normal solution to such ambiguities is to use qualification and write
Art'(F(3)) as in
A := Art'(F(3))(7); -- illegal in Ada 2005
but this is illegal
in Ada 2005 because a qualified expression is not a name
and so cannot be used as a prefix. What one
has to do in Ada 2005 is either copy the wretched array (really naughty)
or add a type conversion (a type conversion is a name)
thus
A := Art(Art'(F(3)))(7);
This is really gruesome;
but in Ada 2012, qualification is permitted as a name
so we can simply write
A := Art'(F(3))(7); -- OK in Ada 2012
Although a qualified expression is now classed as
a name rather than a primary,
a qualified variable is not considered to be a variable. As a consequence,
a qualified variable cannot be used as the destination of an assignment
or as an actual parameter corresponding to an out or in out
parameter. This would have added complexity for no useful purpose. Ambiguity
generally involves calls on overloaded functions, and the result of a
function call is always a constant, so ambiguous names of variables are
unlikely!
Other uses might involve
strings which can also give rise to ambiguities. For example
("a string")'Length
is ambiguous (it could be a String
or Wide_String).
But now we can write
String'("a string")'Length
which was not permitted in Ada 2005.
© 2011, 2012, 2013 John Barnes Informatics.
Sponsored in part by: