Summary
This short chapter describes additional forms of expressions introduced in Ada 2012. These are related to the three bracketed sequential control structures of Ada described in the previous chapter. Thus, corresponding to the if statement there is the if expression and corresponding to the case statement there is the case expression. There are also quantified expressions and these are related to loop statements. We also discuss the new forms of membership tests in Ada 2012.
It is perhaps very surprising that Ada did not have conditional forms of expressions right from the beginning. Even Algol 60 had conditional expressions. However, perhaps there was a feeling that conditional expressions could lead one astray by loosening the crisp distinction between statements and expressions.
The incentive to introduce conditional expressions into Ada was triggered by the addition of pre- and postconditions which are described in Chapter 16. Without adding conditional forms of expressions these pre- and postconditions would have been cumbersome and required lots of small functions. However, these additional forms of expressions are applicable in other situations as well and it seems appropriate to describe them here.
Membership tests
Membership tests were introduced in Section 6.9 where we showed how they could be used to check whether a value lies within a specified range or satisfies a constraint implied by a subtype.
Thus given I of type Integer we could write
if I in 1.. 10 then …
and given the type Day describing all the days of the week and the subtype Weekday we could test whether a variable D of the type Day had a value within the subtype by writing
if D in Weekday then …
These were the only forms of membership test allowed in Ada 2005, but more general forms are permitted in Ada 2012.
Suppose we have an enumeration type for the names of months and perhaps related subtypes thus
type Month_Name is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
subtype Spring is Month_Name range March .. May;
and wish to check whether it is safe to eat an oyster (there has to be an R in the month). In Ada 2005 we can write
if M in Jan .. April or M in Sep .. Dec then
which means repeating M and then perhaps worrying about whether to use or or or else.
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 149 - 160Publisher: Cambridge University PressPrint publication year: 2014