# Ada Programming/Types/record

A record is a composite type that groups one or more fields. A field can be of any type, even a record.

## Null record

The null record is when a type without data is needed. There are two ways to declare a null record:
For the compiler they are the same. However, programmers often use the first variant if the type is not finished yet to show that they are planning to expand the type later, or they usually use the second if the (tagged) record is a base class in object oriented programming.

## Record Values

Values of a record type can be specified using a record aggregate, giving a list of named components thus
Given a somewhat larger record type,
a value may be specified using positional notation, that is, specifying a value for each record component in declaration order
However, naming the components of a Car aggregate offers a number of advantages.
* Easy identification of which value is used for which component. (After all, named components are the raison d'être of records.)
* Reordering the components is allowed—you only have to remember the component names, not their position.
* Improved compiler diagnostic messages.
Reordering components is possible because component names will inform the compiler (and the human reader!) of the intended value associations. Improved compiler messages are also in consequence of this additional information passed to the compiler. While an omitted component will always be reported due to Ada's coverage rules, messages can be much more specific when there are named associations. Considering the Car type from above, suppose a programmer by mistake specifies only one of the two floating point values for BMW in positional notation. The compiler, in search of another component value, will then not be able to decide whether the specified value is intended for Horse_Power_kW or for Consumption. If the programmer instead uses named association, say Horse_Power_kW => 190.0, it will be clear which other component is missing.
In order to access a component of a record instance, use the dot delimiter (.), as in BMW.Number_Wheels.

## Variant record

The variant record is a special type of discriminated record where the presence of some components depend on the value of the discriminant.

### Mutable and immutable variant records

You can declare variant record types such that its discriminant, and thus its variant structure, can be changed during the lifetime of the variable. Such a record is said to be mutable. When "mutating" a record, you must assign all components of the variant structure which you are mutating at once, replacing the record with a complete variant structure. Although a variant record declaration may allow objects of its type to be mutable, there are certain restrictions on whether the objects will be mutable. Reasons restricting an object from being mutable include:
* the object is declared with a discriminant (see Immutable_Traffic_Light below)
* the object is aliased (either by use of aliased in the object declaration, or by allocation on the heap using new)
Conversely, you can declare record types so that the discriminant along with the structure of the variant record may not be changed. To make a record type declaration immutable, the discriminant must not have a default value.

## Union

This language feature is only available from Ada 2005 on.
The difference to a variant record is such that Option is not actually stored inside the record and never checked for correctness - it's just a dummy.
This kind of record is usually used for interfacing with C but can be used for other purposes as well (then without pragma Convention (C, Union);).

## Tagged record

The tagged record is one part of what in other languages is called a class. It is the basic foundation of object orientated programming in Ada. The other two parts a class in Ada needs is a package and primitive operations.
Ada 2005 only:

## Abstract tagged record

An abstract type has at least one abstract primitive operation, i.e. one of its operations is not defined and implementation must be provided by derivatives of the abstract type.

## With aliased elements

If you come from C/C++, you are probably used to the fact that every element of a record - which is not part of a bitset - has an address. In Ada, this is not true because records, just like arrays, can be packed. And just like arrays you can use aliased to ensure that an element can be accessed via an access type.
Please note: each element needs its own aliased.

## Limited Records

In addition to being variant, tagged, and abstract, records may also be limited (no assignment, and no predefined equality operation for Limited Types). In object oriented programming, when tagged objects are handled by references instead of copying them, this blends well with making objects limited.

## See also
### Wikibook

* Ada Programming
* Ada Programming/Types
* Ada Programming/Keywords/record
* Ada Programming/Keywords/null
* Ada Programming/Keywords/abstract
* Ada Programming/Keywords/case
* Ada Programming/Keywords/when
* Ada Programming/Pragmas/Unchecked Union

### Ada Reference Manual
#### Ada 95

* 3.8: Record Types

#### Ada 2005

* 3.8: Record Types
* B.3.3: Pragma Unchecked_Union

#### Ada Issues

* AI95-00216-01 Unchecked unions — variant records with no run-time discriminant