9. Records - JulTob/Ada GitHub Wiki

Records must be declared before use.

procedure record_example is

type record_type_name 
  is record
    Element1: Type1;
    Element2: Type2;
    ElementN: TypeN;
     end record;

object: Record_Type_Name;
object2: Record_Type_Name := (Val1, val2, valN);
object3: Record_Type_Name := (Type1 => Val1, Type2 => val2, TypeN => valN);
object4: Record_Type_Name := (TypeN => ValN, Type1 => val1, Type2 => val2);
object4: Record_Type_Name 
       := (TypeN => ValN,
           others => <> -- Others, default values 
           );
begin

-- Assign
object.ElementN:= ValueN;
-- Calls
put(object.ElementN);

end record_example;
type Record_Type (discriminant : disc_type) is
record
  --  Declarations
end record;  

Example:

type Record_Type (size : Positive := 1) is
record
  ID: Positive;
  Text : String(1..Size);
end record;  

Examples

Type Buffer_type is record
   Data: String(1..80);
   Start, Finish: Positive;
end record;
--...--
My_Buffer: Buffer_type;
--...--
My_Buffer.Start:=1;
My_Buffer.Finish:=1;
My_Buffer.Data(1..3) := "ABC";
My_Buffer:= ( Data=>('X','Y','Z', others=>' '), 
   Start => 1, 
   Finish => 3);
type Date is
record 
    Day  : Integer range 1 .. 31;
    Month: Integer range 1 .. 12;
    Year : Natural;
end record
...
Today: Date := (Day =>1, Month => 7, Year => 2017);
...
Tomorrow := Today;
Tomorrow.Day= Today.Day + 1;
Type student is record
   Firstname : string(1 .. 24);
   Lastname : string(1 .. 24);
   Age : integer;
   Telephone : string( 1 .. 13) := "0523-xx-xx-xx";
End record;

Type School is array(positive range <>) of student;

South_School : school(1..100);
--...--
South_School(27).telephone := my_phone;

type Car_Type 
  is record
    Years_Owned: Natural:= 0;
    Kilometers:  Natural:= 0;
    TopSpeed: Float:= 0.0;
    Price: Float:= 0.0;
    Model: String(1..16) := (others => ' ');
  end record;

Arrays of records

with Ada.String.Unbounded;

procedure students_List is

type Student is record
   Name: Ada.Strings.Unbounded.Unbounded_String := Ada.Strings.Unbounded.Null_Unbounded_String;
   Average: Float:= 50.0;
   Passed: Boolean := False;
 
   end Record;

class: array(1..15) of Student;
begin

class(1).Name:= Ada.Strings.Unbounded.To_Unbounded_String("Jack");
class(1).Average:= 75.2;
class(1).Passed:= True;
    Variant                 The variant part of a record specifies
                            alternative record components, depending
                            on a discriminant of the record.  Each
                            value of the discriminant establishes
                            a particular alternative of the variant
                            part.

Mutants

A record can be mutant according to a parameter

type Pokemon( t : Type) 
   is record
   strength: Natural := 100;
   HP: Natural := 1_000;
   case t is
      when electric =>
         Attack : STRING := "Thunderbolt";
      when t is fire => 
         Attack : String := "Fireball";
      when  t is water =>
         Attack : String := "Watercannon"; 
         Surf: Boolean := TRUE;
   end record;

Un agregado de record especifica los valores de los componentes de un record, bien asociándolos por posición, bien asociándolos por nombre. Cuando se usa un agregado nominal los campos se pueden enumerar en cualquier orden, pero siempre hay que enumerarlos todos.

type Complejo is record
    Real, Imag: Float := 0.0;
    end record;

X : Complejo;
X := (3.5, 7.1);                 --  Asociación posicional
X := (Real => 3.5, Imag => 7.1); --  Asociación nominal
X := (others => 0.0);
X := (others => <>);             --  Valor por omisión

Los record son estructuras heterogéneas: agregados de elementos (campos) del mismo o distintos tipos que se pueden acceder individualmente mediante su nombre. Un record se define con la palabra record, seguida de la declaración de los campos del record y end record.

type Complejo is
record
    Real, Imag: Float := 0.0;
    end record;

type Record_Nulo is null record;

Se usa un punto (.) para seleccionar componentes.

type Complejo is record
    Real, Imag: Float := 0.0;
    end record;
X : Complejo;
X.Real := 3.5; -- Selección del componente "Real"
X.Imag := 7.0; -- Selección del componente "Imag"