Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
966 changes: 43 additions & 923 deletions courses/fundamentals_of_ada/060_record_types.rst

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions courses/fundamentals_of_ada/060_record_types/01-introduction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
==============
Introduction
==============

---------------------
Syntax and Examples
---------------------

* Syntax (simplified)

.. code:: Ada

type T is record
Component_Name : Type [:= Default_Value];
...
end record;

type T_Empty is null record;

* Example

.. code:: Ada

type Record1_T is record
Field1 : Integer;
Field2 : Boolean;
end record;

* Records can be **discriminated** as well

.. code:: Ada

type T (Size : Natural := 0) is record
Text : String (1 .. Size);
end record;

141 changes: 141 additions & 0 deletions courses/fundamentals_of_ada/060_record_types/02-components_rules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
==================
Components Rules
==================

-------------------------------
Characteristics of Components
-------------------------------

* **Heterogeneous** types allowed
* Referenced **by name**
* May be no components, for **empty records**
* **No** anonymous types (e.g., arrays) allowed

.. code:: Ada

type Record_1 is record
This_Is_Not_Legal : array (1 .. 3) of Integer;
end record;

* **No** constant components

.. code:: Ada

type Record_2 is record
This_Is_Not_Legal : constant Integer := 123;
end record;

* **No** recursive definitions

.. code:: Ada

type Record_3 is record
This_Is_Not_Legal : Record_3;
end record;

* **No** indefinite types

.. code:: Ada

type Record_5 is record
This_Is_Not_Legal : String;
But_This_Is_Legal : String (1 .. 10);
end record;

-----------------------
Multiple Declarations
-----------------------

* Multiple declarations are allowed (like objects)

.. code:: Ada

type Several is record
A, B, C : Integer := F;
end record;

* Equivalent to

.. code:: Ada

type Several is record
A : Integer := F;
B : Integer := F;
C : Integer := F;
end record;

-----------------------------------------
"Dot" Notation for Components Reference
-----------------------------------------

.. code:: Ada

type Months_T is (January, February, ..., December);
type Date is record
Day : Integer range 1 .. 31;
Month : Months_T;
Year : Integer range 0 .. 2099;
end record;
Arrival : Date;
...
Arrival.Day := 27; -- components referenced by name
Arrival.Month := November;
Arrival.Year := 1990;

* Can reference nested components

.. code:: Ada

Employee
.Birth_Date
.Month := March;

------
Quiz
------

..
This file is auto-generated from the quiz template, it should not be modified
directly. Read README.md for more information.

.. code:: Ada

type Record_T is record
-- Definition here
end record;

Which record definition(s) is (are) legal?

A. ``Component_1 : array (1 .. 3) of Boolean``
B. :answermono:`Component_2, Component_3 : Integer`
C. ``Component_1 : Record_T``
D. ``Component_1 : constant Integer := 123``

.. container:: animate

A. Anonymous types not allowed
B. Correct
C. No recursive definition
D. No constant component
.. include:: ../quiz/record_component_decl/quiz.rst

------
Quiz
------

.. code:: Ada

type Cell is record
Val : Integer;
Message : String;
end record;

Is the definition legal?

A. Yes
B. :answer:`No`

.. container:: animate

A :ada:`record` definition cannot have a component of an indefinite type. :ada:`String` is indefinite if you don't specify its size.

74 changes: 74 additions & 0 deletions courses/fundamentals_of_ada/060_record_types/03-operations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
============
Operations
============

----------------------
Available Operations
----------------------

* Predefined

- Equality (and thus inequality)

.. code:: Ada

if A = B then

- Assignment

.. code:: Ada

A := B;

* User-defined

- Subprograms

---------------------
Assignment Examples
---------------------

.. code:: Ada

declare
type Complex is record
Real : Float;
Imaginary : Float;
end record;
...
Phase1 : Complex;
Phase2 : Complex;
begin
...
-- object reference
Phase1 := Phase2; -- entire object reference
-- component references
Phase1.Real := 2.5;
Phase1.Real := Phase2.Real;
end;

-----------------------------
Limited Types - Quick Intro
-----------------------------

* A :ada:`record` type can be limited

- And some other types, described later

* :dfn:`limited` types cannot be **copied** or **compared**

- As a result then cannot be assigned
- May still be modified component-wise

.. code:: Ada

type Lim is limited record
A, B : Integer;
end record;

L1, L2 : Lim := Create_Lim (1, 2); -- Initial value OK

L1 := L2; -- Illegal
if L1 /= L2 then -- Illegal
[...]

Loading