Records

Record Objects

Also known as composed classes. They are really simple:

Records are new types composed from previously existing types as parts of the new object.

type Date is
 record
   Day, Month, Year : Integer;
end record
my_date : Date := (12, 01, 09);
--  Ok
my_date := (
   Day => 12, 
   Month => 01, 
   Year => 09);
-- Great!1

Example

A date is composed of three numbers: One day for the Day, another for the Month, and a third one for the Year.

We can easily set a new variable with the values for the records.

In this case we want to set it to 12th of January of 2009

You may not know that the ISO standard, dates should be displayed YY, MM, DD and even if in Europe the standard format is DD, MM, YY in USA it is MM, DD, YY.

This can be a little confusing if you don't know who programmed the record type. That's why Ada allows for named initialization.

type Soldier is record
   Name : String(1..30) := (others <= ' ');
   Age  : Positive := 18;
end record
S: Soldier;
...
S.Name(1..5) := "James";
S.Age := 26;
 type Unit is array (1..10) of Soldier;
condor : Unit;
...
condor(1).Name(1..4) := "John";
condor(1).Age := 28;

Fields can be initialized with a default value.

They can also be used as elements for an array.