Skip to content

Commit 411f2f1

Browse files
Merge branch 'slides/136-break-060_record_types-into-chapters' into 'master'
Resolve "Break 060_record_types into chapters" Closes #136 See merge request feng/training/material!223
2 parents 460877d + 80a50d7 commit 411f2f1

File tree

9 files changed

+952
-923
lines changed

9 files changed

+952
-923
lines changed

courses/fundamentals_of_ada/060_record_types.rst

Lines changed: 43 additions & 923 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
---------------------
6+
Syntax and Examples
7+
---------------------
8+
9+
* Syntax (simplified)
10+
11+
.. code:: Ada
12+
13+
type T is record
14+
Component_Name : Type [:= Default_Value];
15+
...
16+
end record;
17+
18+
type T_Empty is null record;
19+
20+
* Example
21+
22+
.. code:: Ada
23+
24+
type Record1_T is record
25+
Field1 : Integer;
26+
Field2 : Boolean;
27+
end record;
28+
29+
* Records can be **discriminated** as well
30+
31+
.. code:: Ada
32+
33+
type T (Size : Natural := 0) is record
34+
Text : String (1 .. Size);
35+
end record;
36+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
==================
2+
Components Rules
3+
==================
4+
5+
-------------------------------
6+
Characteristics of Components
7+
-------------------------------
8+
9+
* **Heterogeneous** types allowed
10+
* Referenced **by name**
11+
* May be no components, for **empty records**
12+
* **No** anonymous types (e.g., arrays) allowed
13+
14+
.. code:: Ada
15+
16+
type Record_1 is record
17+
This_Is_Not_Legal : array (1 .. 3) of Integer;
18+
end record;
19+
20+
* **No** constant components
21+
22+
.. code:: Ada
23+
24+
type Record_2 is record
25+
This_Is_Not_Legal : constant Integer := 123;
26+
end record;
27+
28+
* **No** recursive definitions
29+
30+
.. code:: Ada
31+
32+
type Record_3 is record
33+
This_Is_Not_Legal : Record_3;
34+
end record;
35+
36+
* **No** indefinite types
37+
38+
.. code:: Ada
39+
40+
type Record_5 is record
41+
This_Is_Not_Legal : String;
42+
But_This_Is_Legal : String (1 .. 10);
43+
end record;
44+
45+
-----------------------
46+
Multiple Declarations
47+
-----------------------
48+
49+
* Multiple declarations are allowed (like objects)
50+
51+
.. code:: Ada
52+
53+
type Several is record
54+
A, B, C : Integer := F;
55+
end record;
56+
57+
* Equivalent to
58+
59+
.. code:: Ada
60+
61+
type Several is record
62+
A : Integer := F;
63+
B : Integer := F;
64+
C : Integer := F;
65+
end record;
66+
67+
-----------------------------------------
68+
"Dot" Notation for Components Reference
69+
-----------------------------------------
70+
71+
.. code:: Ada
72+
73+
type Months_T is (January, February, ..., December);
74+
type Date is record
75+
Day : Integer range 1 .. 31;
76+
Month : Months_T;
77+
Year : Integer range 0 .. 2099;
78+
end record;
79+
Arrival : Date;
80+
...
81+
Arrival.Day := 27; -- components referenced by name
82+
Arrival.Month := November;
83+
Arrival.Year := 1990;
84+
85+
* Can reference nested components
86+
87+
.. code:: Ada
88+
89+
Employee
90+
.Birth_Date
91+
.Month := March;
92+
93+
------
94+
Quiz
95+
------
96+
97+
..
98+
This file is auto-generated from the quiz template, it should not be modified
99+
directly. Read README.md for more information.
100+
101+
.. code:: Ada
102+
103+
type Record_T is record
104+
-- Definition here
105+
end record;
106+
107+
Which record definition(s) is (are) legal?
108+
109+
A. ``Component_1 : array (1 .. 3) of Boolean``
110+
B. :answermono:`Component_2, Component_3 : Integer`
111+
C. ``Component_1 : Record_T``
112+
D. ``Component_1 : constant Integer := 123``
113+
114+
.. container:: animate
115+
116+
A. Anonymous types not allowed
117+
B. Correct
118+
C. No recursive definition
119+
D. No constant component
120+
.. include:: ../quiz/record_component_decl/quiz.rst
121+
122+
------
123+
Quiz
124+
------
125+
126+
.. code:: Ada
127+
128+
type Cell is record
129+
Val : Integer;
130+
Message : String;
131+
end record;
132+
133+
Is the definition legal?
134+
135+
A. Yes
136+
B. :answer:`No`
137+
138+
.. container:: animate
139+
140+
A :ada:`record` definition cannot have a component of an indefinite type. :ada:`String` is indefinite if you don't specify its size.
141+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
============
2+
Operations
3+
============
4+
5+
----------------------
6+
Available Operations
7+
----------------------
8+
9+
* Predefined
10+
11+
- Equality (and thus inequality)
12+
13+
.. code:: Ada
14+
15+
if A = B then
16+
17+
- Assignment
18+
19+
.. code:: Ada
20+
21+
A := B;
22+
23+
* User-defined
24+
25+
- Subprograms
26+
27+
---------------------
28+
Assignment Examples
29+
---------------------
30+
31+
.. code:: Ada
32+
33+
declare
34+
type Complex is record
35+
Real : Float;
36+
Imaginary : Float;
37+
end record;
38+
...
39+
Phase1 : Complex;
40+
Phase2 : Complex;
41+
begin
42+
...
43+
-- object reference
44+
Phase1 := Phase2; -- entire object reference
45+
-- component references
46+
Phase1.Real := 2.5;
47+
Phase1.Real := Phase2.Real;
48+
end;
49+
50+
-----------------------------
51+
Limited Types - Quick Intro
52+
-----------------------------
53+
54+
* A :ada:`record` type can be limited
55+
56+
- And some other types, described later
57+
58+
* :dfn:`limited` types cannot be **copied** or **compared**
59+
60+
- As a result then cannot be assigned
61+
- May still be modified component-wise
62+
63+
.. code:: Ada
64+
65+
type Lim is limited record
66+
A, B : Integer;
67+
end record;
68+
69+
L1, L2 : Lim := Create_Lim (1, 2); -- Initial value OK
70+
71+
L1 := L2; -- Illegal
72+
if L1 /= L2 then -- Illegal
73+
[...]
74+

0 commit comments

Comments
 (0)