So far, all the types we have encountered have values that are not decomposable: each instance represents a single piece of data. Now we are going to see our first class of composite types: records.
Records allow composing a value out of instances of other types. Each of those instances will be given a name. The pair consisting of a name and an instance of a specific type is called a field, or a component.
Record type declaration
Here is an example of a simple record declaration:
type Date is record -- The following declarations are -- components of the record Day : Integer range 1 .. 31; Month : Months; -- You can add custom constraints -- on fields Year : Integer range 1 .. 3000; end record;
Fields look a lot like variable declarations, except that they are inside of a record definition. And as with variable declarations, you can specify additional constraints when supplying the subtype of the field.
type Date is record Day : Integer range 1 .. 31; Month : Months := January; -- This component has a default value Year : Integer range 1 .. 3000 := 2012; -- ^ Default value end record;
Record components can have default values. When a variable having the record type is declared, a field with a default initialization will be automatically set to this value. The value can be any expression of the component type, and may be run-time computable.
In the remaining sections of this chapter, we see how to use record types. In addition to that, we discuss more about records in another chapter.
Ada_Birthday : Date := (10, December, 1815); Leap_Day_2020 : Date := (Day => 29, Month => February, Year => 2020); -- ^ By name
Records have a convenient notation for expressing values, illustrated above. This notation is called aggregate notation, and the literals are called aggregates. They can be used in a variety of contexts that we will see throughout the course, one of which is to initialize records.
An aggregate is a list of values separated by commas and enclosed in parentheses. It is allowed in any context where a value of the record is expected.
Values for the components can be specified positionally, as in
Ada_Birthday example, or by name, as in
Leap_Day_2020. A mixture
of positional and named values is permitted, but you cannot use a positional
notation after a named one.
To access components of a record instance, you use an operation that is
called component selection. This is achieved by using the dot notation. For
example, if we declare a variable
Some_Day of the
type mentioned above, we can access the
Year component by writing
Let's look at an example:
As you can see in this example, we can use the dot notation in the expression
Some_Day.Year to access the information stored in that
component, as well as to modify this information in assignments. To be more
specific, when we use
D.Year in the call to
retrieving the information stored in that component. When we write
Some_Day.Year := 2001, we're overwriting the information that was
previously stored in the
Year component of
In previous chapters, we've discussed subprogram and package renaming. We can rename record components as well. Instead of writing the full component selection using the dot notation, we can declare an alias that allows us to access the same component. This is useful to simplify the implementation of a subprogram, for example.
We can rename record components by using the
renames keyword in a
variable declaration. For example:
Some_Day : Date; Y : Integer renames Some_Day.Year;
Y is an alias, so that every time we using
Y, we are really
Year component of
Let's look at a complete example:
We apply renaming to two components of the
Date record in the
implementation of the
Increase_Month procedure. Then, instead of
Some_Day.Year in the
next operations, we simply use the renamed versions
Note that, in the example above, we also rename
which is the function that gives us the next month — to