-- Chapter 30 - Programming exercise 3 generic type ITEM is private; procedure Exchange_Data(X, Y : in out ITEM); procedure Exchange_Data(X, Y : in out ITEM) is Temp : ITEM; begin Temp := X + 1; -- This should produce an error X := Y; Y := Temp; end Exchange_Data; generic type ANOTHER_ITEM is range <>; -- Integer class only function Average(X, Y : ANOTHER_ITEM) return ANOTHER_ITEM; function Average(X, Y : ANOTHER_ITEM) return ANOTHER_ITEM is Temporary : ANOTHER_ITEM; begin Temporary := (X + Y) / 2; return Temporary; end Average; -- This is the beginning of the main program which uses the generic -- procedure and function defined above. with Exchange_Data, Average; with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO; procedure CH30_3 is type NEW_TYPE is new INTEGER range 122..12345; type MY_RECORD is record My_Grade : INTEGER range 1..128; My_Class : CHARACTER; end record; procedure Swap is new Exchange_Data(INTEGER); procedure Swap is new Exchange_Data(MY_RECORD); procedure Swap is new Exchange_Data(FLOAT); procedure Swap is new Exchange_Data(NEW_TYPE); procedure Swap is new Exchange_Data(CHARACTER); procedure Trade is new Exchange_Data(CHARACTER); procedure Exchange is new Exchange_Data(CHARACTER); procedure Puppy is new Exchange_Data(CHARACTER); function Mean is new Average(INTEGER); function Mean is new Average(NEW_TYPE); function Swap is new Average(INTEGER); -- This is dumb to do, -- but it is legal. Index1 : INTEGER := 117; Index2 : INTEGER := 123; Data1 : MY_RECORD := (15,'A'); Data2 : MY_RECORD := (91,'B'); Real1 : FLOAT := 3.14; Real2 : FLOAT := 77.02; Value1 : NEW_TYPE := 222; Value2 : NEW_TYPE := 345; begin Put(Real1, 4, 2, 0); Put(Real2, 4, 2, 0); New_Line; Swap(Real1, Real2); Put(Real1, 4, 2, 0); Put(Real2, 4, 2, 0); New_Line(2); Put(Index1); Put(Index2); New_Line; Swap(Index1, Index2); Swap(Data1, Data2); Put(Index1); Put(Index2); New_Line; -- Now to exercise some of the functions Index1 := Mean(Index2, 16); Value1 := Mean(Value2, Value2 + 132); Index1 := Swap(Index1, Index2 + 12); -- This actually gets the -- mean of the inputs. end CH30_3; -- Result of Execution -- Compile error, -- Line 9 - Illegal operation for a private type, type clash.