Ada: Records

Ada: Records

Custom data types in Ada

Record Type

A record in Ada is a user-defined composite data type that groups variables of different types under a single name. Records allow you to define structures containing multiple related data items.

Some key points about records in Ada:

  • Records are declared using the record keyword followed by the record name and a list of components within the record.

  • Components can be of any valid Ada type - numbers, strings, arrays, other records, etc.

  • Components are accessed using dot notation - record_name.component_name

  • Records can contain other records as components, allowing nested records.

  • Records in Ada support discriminants which are variables that determine the size or contents of a record at runtime.

  • Records can have private and public components, allowing for encapsulation.

For example:

Type Person is record 
   Name  : String(1..20);
   Age   : Integer;  
   Address: Address;  -- Nested record  
end record;

Type Address is record
   Street : String(1..30);  
   City   : String(1..20); 
end record;

Here we have defined two records - Person contains components Name, Age and Address. The Address component is itself a nested record.

We can instantiate and access records as:

Person1 : Person;
Person1.Name := "John";
Person1.Age := 30;
Person1.Address.Street := "Main Street";

So in summary, records in Ada allow you to logically group related data and create structured data types. They provide a clean and organized way of handling data.


Objects

You're right, records in Ada can actually be thought of as a basic form of objects that have some standard methods. Some points to note:

  • Records in Ada support inheritance and can be inherited from other records, allowing a hierarchical record structure. This is similar to object-oriented inheritance.

  • Records can have components that are of access types, allowing them to point to other records. This provides a way to link records together.

  • Records support operations like assignment, equality comparison and aggregation (combining two records into one). These can be thought of as standard methods that all records have.

  • Records can have private components that are hidden from the outside. This provides encapsulation similar to objects.

  • Records in Ada can be tagged, which means they can support user-defined primitive operations (methods). Tagged records can then be used for polymorphism.

  • Tagged records also support controlled types, which are types that have user-defined initialization and finalization routines. This is similar to object constructors and destructors.

So in summary, while records in Ada are more primitive than full-fledged objects, they do support some object-oriented features like inheritance, encapsulation, polymorphism and initialization/finalization. The main differences are:

  • Objects typically have both data (attributes) and behavior (methods), while records mainly focus on grouping data.

  • Objects are manipulated through method calls, while records are accessed directly through dot notation.

But tagged records in Ada come closer to being actual objects by allowing user-defined methods and controlled types. So in that sense, records - especially tagged records - can be thought of as a basic form of objects in Ada.


Recursive Records

Recursive records in Ada are records that contain components of their own type, allowing them to reference themselves. This provides a way to represent self-referential data structures.

Some key points about recursive records in Ada:

  • A recursive record is a record type that contains at least one component whose type is the same as the record type itself. This creates a recursive (self-referential) definition.

  • Recursive records allow representing data structures like linked lists, trees and graphs where nodes contain pointers to other nodes of the same type.

  • Recursive records require the use of access types as components since the record is incomplete at the time of its declaration.

  • When declaring a recursive record, you must first declare an incomplete record specification and then provide a full record definition later.

  • The full record definition can refer to the incomplete record specification, completing the recursion.

For example:

Type Node; -- Incomplete record specification
Type Node_Access is access Node;

Type Node is  
   record
      Value : Integer;
      Next  : Node_Access; -- Node_Access is an access type
   end record;   -- Full record definition

Here Node is a recursive record as it contains a component Next which is an access type pointing to Node itself.

We can then instantiate nodes and link them to form a linked list:

Node1 : Node;
Node2 : Node;
...  
Node1.Next := Node2'Access;
Node2.Next := Node3'Access;
...

So in summary, recursive records allow representing self-referential data structures in Ada by containing a component of their own type. The use of access types makes the recursion possible.


Custom Types

There are several other custom data types you can study for Ada:

  1. Arrays - Ada supports one-dimensional, two-dimensional and multidimensional arrays. Arrays can be of any valid Ada type. Ada also supports unconstrained arrays where the size is determined at runtime.

  2. Strings - Ada has a string type which is an array of characters. It supports operations like concatenation and comparison of strings.

  3. Enumerations - Enumerations allow you to define a custom type consisting of a set of named literals. They are useful for representing a fixed set of possible values.

  4. Pointers - Ada supports access types which act like pointers. They allow storing the address of an object instead of a copy.

  5. Sets - Sets are collections of distinct elements of the same type. Ada supports operations on sets like union, intersection, difference, etc.

  6. Floating point types - Ada has predefined float, long_float and long_long_float types to represent floating point numbers of different precisions.

  7. User-defined numeric types - Ada allows defining your own numeric types using the modular, delta and digits attributes.

  8. Iterators - Ada supports iterators which are objects that provide a way to access the elements of a container sequentially without exposing its underlying representation.

  9. Discriminated records - As mentioned earlier, discriminated records allow the size or contents of a record to vary based on a discriminant.

  10. Tagged types - Tagged types allow defining class-like structures in Ada called tagged types. They support inheritance, polymorphism and dynamic dispatching.

So in summary, in addition to records, Ada supports various other custom data types like arrays, strings, enumerations, pointers, sets, floats, user-defined numeric types, iterators, discriminated records and tagged types. Studying these will help you master the Ada language.


Disclaim: My purpose is te learn the fundamentals of Ada programming using rix. I will let some details for later. There are many other things to discuss before entering in details about all custom data types.