4.5.7 Conditional Expressions
1/3
Language Design Principles
1.a/3
1.b/3
{
AI05-0147-1}
The rules for conditional_expressions
have been designed as much as possible to work similarly to a parenthesized
expression. The intent is that as much as possible, wherever a parenthesized
expression would be allowed, a conditional_expression
would be allowed, and it should work the same way.
Syntax
2/3
3/3
4/3
5/3
6/3
7/3
7.a/3
Discussion: {
AI05-0147-1}
The syntactic category conditional_expression
appears only as a primary that is parenthesized. The above rule allows
it to additionally be used in other contexts where it would be directly
surrounded by parentheses.
7.b/3
The
grammar makes the following directly legal:
7.c/3
A := (if X then Y else Z); -- parentheses required
A := B + (if X then Y else Z) + C; -- parentheses required
7.d/3
The
following procedure calls are syntactically legal; the first uses the
above rule to eliminate the redundant parentheses found in the second:
7.e/3
P(if X then Y else Z);
P((if X then Y else Z)); -- redundant parentheses
7.f/3
P((if X then Y else Z), Some_Other_Param);
P(Some_Other_Param, (if X then Y else Z));
P(Formal => (if X then Y else Z));
7.g/3
whereas
the following are illegal:
7.h/3
P(if X then Y else Z, Some_Other_Param);
P(Some_Other_Param, if X then Y else Z);
P(Formal => if X then Y else Z);
7.i/3
because in these latter
cases, the conditional_expression
is not immediately surrounded by parentheses (which means on both sides!).
7.j/3
The English-language rule
applies in all places that could surround an expression with parentheses,
including pragma arguments, type conversion and qualified expression
operands, and array index expressions.
7.k/3
This English-language
rule could have been implemented instead by adding a non-terminal expression_within_parentheses,
which would consist of expressions
and conditional_expressions.
Then, that could be used in all of the syntax which could consist of
parens directly around an expression.
We did not do that because of the large amount of change required. A
complete grammar is given in AI05-0147-1.
7.l/3
Name Resolution Rules
8/3
9/3
10/3
10.a/3
Reason:
This rule distributes an enclosing type conversion to the dependent_expressions.
This means that
10.b/3
T(if C then A else B)
10.c/3
has
the same semantics as
10.d/3
(if C then T(A) else T(B))
11/3
12/3
12.a/3
Reason: This rule
supports the use of numeric literals and universal expressions within
a conditional_expression.
13/3
13.a/3
Ramification: If
the type of the conditional_expression
cannot be determined by one of these rules, the Name Resolution has failed
for that expression, even if the dependent_expressions
would resolve individually.
14/3
15/3
Legality Rules
16/3
17/3
18/3
19/3
19.a/3
Reason:
The exemption for a case expression that occurs in an instance allows
the following example:
19.b/3
generic
with function Int_Func return Integer;
package G is
X : Float := (case Int_Func is
when Integer'First .. -1 => -1.0,
when 0 => 0.0,
when Positive => 1.0);
end G;
19.c/3
function Nat_Func return Natural is (123);
19.d/3
package I is new G (Int_Func => Nat_Func); -- Legal
19.e/3
Note that the Legality
Rules still apply in the generic unit itself; they are just not enforced
in an instance of the unit.
Dynamic Semantics
20/3
20.a/3
Ramification: Else
is required unless the if_expression
has a boolean type, so the last sentence can only apply to if_expressions
with a boolean type.
21/3
Extensions to Ada 2005
21.a/3
{
AI05-0147-1}
If expressions and case expressions
are new.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe