[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 3.5: Aggregates

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 3: Classical Programming

3.5 Aggregates

For an array or record type, Ada provides a basic operation that combines component values into a composite value of the type. This basic operation is expressed by an aggregate and there are two possible syntaxes for this construct.

The first is the traditional positional notation, in which the composite value is defined by an ordered sequence of component values: each value is implicitly associated with the component that has the same position in the array or record.

The second - and often preferable - notation provided in Ada is the so-called named notation, in which the component-value pairings are given explicitly by component associations of the form

    component => value

using the names of the components (in the simplest case).

The examples given below show that named notation is often much more readable than positional notation, since it allows more explicit formulation of the intent. It is also more reliable, being insensitive to order and permutation. Consider for example the following type:

type DATE is
  record
    MONTH  :  INTEGER range 1 .. 12;
    DAY    :  INTEGER range 1 .. 31;
    YEAR   :  INTEGER range 1 .. 3000;
  end record;

Then we can define dates by positional aggregates such as

    (12, 4, 1983)

but we can express our intent in a more explicit way if we use named notation as in the three equivalent aggregates given below:

(MONTH  =>  12,       DAY   =>  4,        YEAR =>  1983) -- the American way
(DAY    =>  4, MONTH  =>  12,     YEAR    =>  1983)      -- the European way
(YEAR   =>  1983,     MONTH =>  12,       DAY  =>  4)    -- the IS0 way

A reformulation of the type DATE is possible in which an enumeration type is used for months. In that case a permutation error such as

    (4, DECEMBER, 1983)

would be detected by a compiler. In the above all-integer formulation, however, the named notation is more reliable since the meaning does not rely on order.

Similar possibilities exist for an array type such as

    type TABLE is array (1 .. 10) of INTEGER;

Given the object declaration

    T :  TABLE;

we can assign to T a positional array aggregate such as

    T :=  (0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

But the intent can be made more explicit by writing the aggregate in any of the equivalent named forms given below

(1 .. 10 =>  0)
(TABLE'FIRST .. TABLE'LAST =>  0)
(T'FIRST .. T'LAST =>  0)
(TABLE'RANGE =>  0)
(T'RANGE =>  0)
(others =>  0)

Other forms of named notation allow the explicit mention of component indexes, as in the following example

    (1 | 3 | 5 | 7 | 9 => 0,   2 | 4 | 6 | 8 | 10 => 1)

Other examples of aggregates will be presented in Chapter 4 when we discuss overloading resolution for aggregates.


NEXTPREVIOUSUPTOCINDEX
Address any questions or comments to adainfo@sw-eng.falls-church.va.us.