Skip to content

Commit 1e5ea09

Browse files
Merge branch 'slides/184-genericity-simplify-quiz-answer-explanations-for-generic-formal-data-quiz' into 'master'
Resolve "Genericity: Simplify quiz answer explanations for "Generic Formal Data" quiz" Closes #184 See merge request feng/training/material!253
2 parents 9ade407 + b9dc3f0 commit 1e5ea09

File tree

1 file changed

+28
-59
lines changed

1 file changed

+28
-59
lines changed

courses/fundamentals_of_ada/160_genericity/04-generic_formal_data_in_depth.rst

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -108,77 +108,46 @@ Quiz
108108
Quiz
109109
------
110110

111-
.. container:: columns
111+
Given the following generic function:
112112

113-
.. container:: column
113+
.. code:: Ada
114114
115+
generic
116+
type Some_T is private;
117+
with function "+" (L : Some_T; R : Integer) return Some_T is <>;
118+
function Incr (Param : Some_T) return Some_T;
115119
116-
.. code:: Ada
117-
:number-lines: 1
120+
function Incr (Param : Some_T) return Some_T is
121+
begin
122+
return Param + 1;
123+
end Incr;
118124
119-
procedure Double (X : in out Integer);
120-
procedure Square (X : in out Integer);
121-
procedure Half (X : in out Integer);
122-
generic
123-
with procedure Double (X : in out Integer) is <>;
124-
with procedure Square (X : in out Integer) is null;
125-
procedure Math (P : in out Integer);
126-
procedure Math (P : in out Integer) is
127-
begin
128-
Double (P);
129-
Square (P);
130-
end Math;
131-
procedure Instance is new Math (Double => Half);
132-
Number : Integer := 10;
125+
And the following declarations:
133126

134-
.. container:: column
127+
.. code:: Ada
135128
136-
.. container:: column
129+
type Record_T is record
130+
Field : Integer;
131+
end record;
132+
function Add (L : Record_T; I : Integer) return Record_T is
133+
((Field => L.Field + I))
134+
function Weird (L : Integer; R : Integer) return Integer is (0);
137135
138-
What is the value of Number after calling :ada:`Instance (Number)`
136+
Which of the following instantiation(s) is/are **not** legal?
139137

140-
A. 20
141-
B. 400
142-
C. :answer:`5`
143-
D. 10
138+
A. ``function IncrA is new Incr (Integer, Weird);``
139+
B. ``function IncrB is new Incr (Record_T, Add);``
140+
C. :answermono:`function IncrC is new Incr (Record_T);`
141+
D. ``function IncrD is new Incr (Integer);``
144142

145143
.. container:: animate
146144

147-
A. Would be correct for :ada:`procedure Instance is new Math;`
148-
149-
B. Would be correct for either :ada:`procedure Instance is new Math (Double, Square);` *or* :ada:`procedure Instance is new Math (Square => Square);`
150-
151-
C. Correct
152-
153-
* We call formal parameter :ada:`Double`, which has been assigned to actual subprogram :ada:`Half`, so :ada:`P`, which is 10, is halved.
154-
155-
* Then we call formal parameter :ada:`Square`, which has no actual subprogram, so it defaults to :ada:`null`, so nothing happens to :ada:`P`
145+
:ada:`with function "+" (L : Some_T; R : Integer) return Some_T is <>;` indicates that if no function for :ada:`+` is passed in, find (if possible) a matching definition at the point of instantiation.
156146

157-
D. Would be correct for either :ada:`procedure Instance is new Math (Double, Half);` *or* :ada:`procedure Instance is new Math (Square => Half);`
147+
A. :ada:`Weird` matches the subprogram profile, so :ada:`Incr` will use :ada:`Weird` when doing addition for :ada:`Integer`
148+
B. :ada:`Add` matches the subprogram profile, so :ada:`Incr` will use :ada:`Add` when doing the addition for :ada:`Record_T`
149+
C. There is no matching :ada:`+` operation for :ada:`Record_T`, so that instantiation fails to compile
150+
D. Because there is no parameter for the generic formal parameter :ada:`+`, the compiler will look for one in the scope of the instantiation. Because the instantiating type is numeric, the inherited :ada:`+` operator is found
158151

159152
..
160153
language_version 2005
161-
162-
----------------------
163-
Quiz Answer in Depth
164-
----------------------
165-
166-
A. Wrong - result for :ada:`procedure Instance is new Math;`
167-
B. Wrong - result for :ada:`procedure Instance is new Math (Double, Square);`
168-
C. :ada:`Double` at line 10 is mapped to :ada:`Half` at line 3, and :ada:`Square` at line 11 wasn't specified so it defaults to :ada:`null`
169-
D. Wrong - result for :ada:`procedure Instance is new Math (Square => Half);`
170-
171-
.. container:: animate
172-
173-
.. container:: latex_environment tiny
174-
175-
:ada:`Math` is going to call two subprograms in order, :ada:`Double` and :ada:`Square`, but both of those come from the formal data.
176-
177-
Whatever is used for :ada:`Double`, will be called by the :ada:`Math` instance. If nothing is passed in, the compiler tries to find a subprogram named :ada:`Double` and use that. If it doesn't, that's a compile error.
178-
179-
Whatever is used for :ada:`Square`, will be called by the :ada:`Math` instance. If nothing is passed in, the compiler will treat this as a null call.
180-
181-
In our case, :ada:`Half` is passed in for the first subprogram, but nothing is passed in for the second, so that call will just be null.
182-
183-
So the final answer should be 5 (hence letter C).
184-

0 commit comments

Comments
 (0)