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.

Basic record
Basic_Record A : Integer; ;

Null record
The null record is when a type without data is needed. There are two ways to declare a null record:

Null_Record ;    ;

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

A_Basic_Record       Basic_Record          Basic_RecordA  42 Another_Basic_Record Basic_Record          A  42 Nix                   Null_Record

Given a somewhat larger record type,

Car Identity       Long_Long_Integer Number_Wheels  Positive  1  10 Paint          Color Horse_Power_kW Float  00  2_0000 Consumption    Float  00  1000

a value may be specified using positional notation, that is, specifying a value for each record component in declaration order

BMW Car  2007_752_83992434 5 Blue 1900 101

However, naming the components of a Car aggregate offers a number of advantages.
 * 1) Easy identification of which value is used for which component. (After all, named components are the raison d'être of records.)
 * 2) Reordering the components is allowed—you only have to remember the component names, not their position.
 * 3) 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 1900 it will be clear which other component is missing.

BMW Car Identity       2007_752_83992434 Number_Wheels  5 Horse_Power_kW 1900 Consumption    101 Paint          Blue

In order to access a component of a record instance, use the dot delimiter, as in BMWNumber_Wheels.

Discriminated record
Discriminated_Record (Size : Natural) A : String (1 .. Size); ; ...  Item : Discriminated_Record := (Size => Value'Length, A => Value);

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.

Traffic_Light (Red, Yellow, Green); Variant_Record (Option : Traffic_Light) Option Red => Yellow => Green => ;     ;

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 in the object declaration, or by allocation on the heap using )

Traffic_Light (Red, Yellow, Green); Mutable_Variant_Record (Option : Traffic_Light := Red) Location : Natural; Option Red => Flashing : Boolean := True; Yellow => Timeout   : Duration := 0.0; Green => Whatever : Positive := 1; ;     ; ... Mutable_Traffic_Light   : Mutable_Variant_Record; Immutable_Traffic_Light : Mutable_Variant_Record (Option => Yellow); ... Mutable_Traffic_Light  := (Option => Yellow,                                                     Location => 54,                                                       Timeout => 2.3); ... Traffic_Light_Access   Mutable_Variant_Record; Any_Traffic_Light      : Traffic_Light_Access := Mutable_Variant_Record; Aliased_Traffic_Light  :  Mutable_Variant_Record;

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.

Traffic_Light (Red, Yellow, Green); Immutable_Variant_Record (Option : Traffic_Light) Location : Natural := 0; Option Red => Flashing : Boolean := True; Yellow => Timeout   : Duration; Green => Whatever : Positive := 1; ;     ; ... Default_Traffic_Light   : Immutable_Variant_Record; Immutable_Traffic_Light : Immutable_Variant_Record (Option => Yellow);

Union
Traffic_Light (Red, Yellow, Green); Union (Option : Traffic_Light := Traffic_Light'First) Option Red => Yellow => Green => ;     ;   (Union); (C, Union);

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 ).

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.

Person Name  : String (1 .. 10); Gender : Gender_Type; ;

Programmer  Person Skilled_In : Language_List; ;

Ada 2005 only:

Programmer  Person Printable Skilled_In : Language_List; ;

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 to ensure that an element can be accessed via an access type.

Basic_Record A : Integer; ;

Please note: each element needs its own.

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.

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 Issues


|Record Programación en Ada/Tipos/Registros