7 Records


Once again Ada provides the same facilities as other languages, plus a few extra. Records can have aggregates, discriminants which allow for variant records, variable sized records and initialized variables.

Simple records

In general the declaration of an record has this form:

For example:

Accessing and setting fields

Accessing fields of a record is identical to that in pascal and C, i.e. the name of the variable is followed by a dot and then the field name.

As with arrays records can be assigned an aggregate, a complete set of values for all record elements.

Alternatively the aggregate can use positional notation where each element of the record is named.

Both notations can be mixed in the one aggregate with the proviso that all positional values precede named values.

Aggregates can also be used in declarations in exactly the same manner, using either notation.

The same value can be assigned to different fields by using the '|' character.

Default values

Fields in records can be given default values that are used whenever a record of that type is created (unless it is initialised with different values).

Constant records

Just like normal variables constant records can be created. In this case all fields should be initialised through the use of an aggregate or default field values.

The fields in a constant record cannot be assigned a value, nor can the record be reassigned a new value.


Ada allows records to contain discriminants. These extra fields help to customise the record further. They give an extra level of abstraction when modelling data; records can be of the same type yet still be different in size or the number of fields.

Variant records

One of the uses of discriminants allows for variant records. In this case the record contains some fields whose existance is dependent upon the value of the discriminant.

For example the discriminant in the following record is the variable vehicle_type

The discriminant can be supplied when a record is being declared.

When the discriminant has the value car, the record contains the fields owner, description and petrol_consumption. Attempting to access fields such as tare and net is illegal and will cause a constraint error to be generated (this can be caught and handled using exceptions).

Constrained records

The records above are said to be constrained, that is the discriminant can never change. The my_bicycle record does not have the fields tare, net petrol_consumption etc. nor does the compiler allocate room for them.

We can declare the records without setting an initial value for the discriminant, in which case the record is said to be unconstrained. In this case the discriminant can take on any value throughout its life. Obviously in this case the compiler has to allocate enough room for the largest record possible.

Aggregates for records with discriminants must include the value of the discriminant as the first value.

Unconstrained records

We can create records that do not have a fixed discriminant, it can change throughout the life of the record. However to do this the discriminant of the record must have a default value.

Here the account record discriminant has a default value of savings.

We can declare a record

which is created as a savings account. We can change the record type later...

Other uses of discriminants

As well as being used as a case selector in a record, discriminants can also be used to specify the length of arrays that are components of the record.

In this case the length of the array is dependent upon the value of the discriminant. As described above the record can be declared constrained or unconstrained.

This text record is the usual implementation used for variable length strings and text processing.

to the index...