Skip to content

Commit 1660a51

Browse files
Merge branch 'slides/141-break-110_private_types-into-chapters' into 'master'
Resolve "Break 110_private_types into chapters" Closes #141 See merge request feng/training/material!224
2 parents ab9ca60 + 79a6f4b commit 1660a51

9 files changed

+752
-746
lines changed

courses/fundamentals_of_ada/110_private_types.rst

Lines changed: 43 additions & 746 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
--------------
6+
Introduction
7+
--------------
8+
9+
* Why does fixing bugs introduce new ones?
10+
* Control over visibility is a primary factor
11+
12+
- Changes to an abstraction's internals shouldn't break users
13+
- Including type representation
14+
15+
* Need tool-enforced rules to isolate dependencies
16+
17+
- Between implementations of abstractions and their users
18+
- In other words, "information hiding"
19+
20+
--------------------
21+
Information Hiding
22+
--------------------
23+
24+
.. container:: columns
25+
26+
.. container:: column
27+
28+
* A design technique in which implementation artifacts are made inaccessible to users
29+
* Based on control of visibility to those artifacts
30+
31+
- A product of "encapsulation"
32+
- Language support provides rigor
33+
34+
* Concept is "software integrated circuits"
35+
36+
.. container:: column
37+
38+
.. image:: interface_vs_implementation.png
39+
:width: 70%
40+
41+
-------
42+
Views
43+
-------
44+
45+
* Specify legal manipulation for objects of a type
46+
47+
- Types are characterized by permitted values and operations
48+
49+
* Some views are implicit in language
50+
51+
- Mode :ada:`in` parameters have a view disallowing assignment
52+
53+
* Views may be explicitly specified
54+
55+
- Disallowing access to representation
56+
- Disallowing assignment
57+
58+
* Purpose: control usage in accordance with design
59+
60+
- Adherence to interface
61+
- Abstract Data Types
62+
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
============================================
2+
Implementing Abstract Data Types Via Views
3+
============================================
4+
5+
----------------------------------
6+
Implementing Abstract Data Types
7+
----------------------------------
8+
9+
* A combination of constructs in Ada
10+
* Not based on single "class" construct, for example
11+
* Constituent parts
12+
13+
- Packages, with "private part" of package spec
14+
- "Private types" declared in packages
15+
- Subprograms declared within those packages
16+
17+
---------------------------------------------
18+
Package Visible and Private Parts for Views
19+
---------------------------------------------
20+
21+
* Declarations in visible part are exported to users
22+
* Declarations in private part are hidden from users
23+
24+
- No compilable references to type's actual representation
25+
26+
.. code:: Ada
27+
28+
package name is
29+
... exported declarations of types, variables, subprograms ...
30+
private
31+
... hidden declarations of types, variables, subprograms ...
32+
end name;
33+
34+
-----------------------------------
35+
Declaring Private Types for Views
36+
-----------------------------------
37+
38+
* Partial syntax
39+
40+
.. code:: Ada
41+
42+
type defining_identifier is private;
43+
44+
* Private type declaration must occur in visible part
45+
46+
- :dfn:`Partial view`
47+
- Only partial information on the type
48+
- Users can reference the type name
49+
50+
- But cannot create an object of that type until after the full type declaration
51+
52+
* Full type declaration must appear in private part
53+
54+
- Completion is the :dfn:`Full view`
55+
- **Never** visible to users
56+
- **Not** visible to designer until reached
57+
58+
.. code:: Ada
59+
60+
package Bounded_Stacks is
61+
type Stack is private;
62+
procedure Push (Item : in Integer; Onto : in out Stack);
63+
...
64+
private
65+
...
66+
type Stack is record
67+
Top : Positive;
68+
...
69+
end Bounded_Stacks;
70+
71+
---------------------------------
72+
Partial and Full Views of Types
73+
---------------------------------
74+
75+
* Private type declaration defines a :dfn:`partial view`
76+
77+
- The type name is visible
78+
- Only designer's operations and some predefined operations
79+
- No references to full type representation
80+
81+
* Full type declaration defines the :dfn:`full view`
82+
83+
- Fully defined as a record type, scalar, imported type, etc...
84+
- Just an ordinary type within the package
85+
86+
* Operations available depend upon one's view
87+
88+
---------------------------------
89+
Software Engineering Principles
90+
---------------------------------
91+
92+
* Encapsulation and abstraction enforced by views
93+
94+
- Compiler enforces view effects
95+
96+
* Same protection as hiding in a package body
97+
98+
- Recall "Abstract Data Machines" idiom
99+
100+
* Additional flexibility of types
101+
102+
- Unlimited number of objects possible
103+
- Passed as parameters
104+
- Components of array and record types
105+
- Dynamically allocated
106+
- et cetera
107+
108+
-----------------------------------
109+
Users Declare Objects of the Type
110+
-----------------------------------
111+
112+
* Unlike "abstract data machine" approach
113+
* Hence must specify which stack to manipulate
114+
115+
- Via parameter
116+
117+
.. code:: Ada
118+
119+
X, Y, Z : Bounded_Stacks.Stack;
120+
...
121+
Push (42, X);
122+
...
123+
if Empty (Y) then
124+
...
125+
Pop (Counter, Z);
126+
127+
------------------------------------
128+
Compile-Time Visibility Protection
129+
------------------------------------
130+
131+
* No type representation details available outside the package
132+
* Therefore users cannot compile code referencing representation
133+
* This does not compile
134+
135+
.. code:: Ada
136+
137+
with Bounded_Stacks;
138+
procedure User is
139+
S : Bounded_Stacks.Stack;
140+
begin
141+
S.Top := 1; -- Top is not visible
142+
end User;
143+
144+
-------------------
145+
Benefits of Views
146+
-------------------
147+
148+
* Users depend only on visible part of specification
149+
150+
- Impossible for users to compile references to private part
151+
- Physically seeing private part in source code is irrelevant
152+
153+
* Changes to implementation don't affect users
154+
155+
- No editing changes necessary for user code
156+
157+
* Implementers can create bullet-proof abstractions
158+
159+
- If a facility isn't working, you know where to look
160+
161+
* Fixing bugs is less likely to introduce new ones
162+
163+
------
164+
Quiz
165+
------
166+
167+
.. code:: Ada
168+
169+
package P is
170+
type Private_T is private;
171+
172+
type Record_T is record
173+
174+
Which component(s) is (are) legal?
175+
176+
A. ``Field_A : Integer := Private_T'Pos (Private_T'First);``
177+
B. ``Field_B : Private_T := null;``
178+
C. ``Field_C : Private_T := 0;``
179+
D. :answermono:`Field_D : Integer := Private_T'Size;`
180+
181+
.. code:: Ada
182+
183+
end record;
184+
185+
.. container:: animate
186+
187+
Explanations
188+
189+
A. Visible part does not know :ada:`Private_T` is discrete
190+
B. Visible part does not know possible values for :ada:`Private_T`
191+
C. Visible part does not know possible values for :ada:`Private_T`
192+
D. Correct - type will have a known size at run-time
193+

0 commit comments

Comments
 (0)