home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / nestpkg.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.8 KB  |  73 lines

  1.                                     -- Chapter 29 - Program 2
  2. package EasyPkg is
  3.    procedure Trade_Values (X, Y : in out INTEGER);
  4.  
  5.    generic
  6.       type MY_REAL_TYPE is digits <>;
  7.    package Nested_Generic is
  8.       function Average_Values (X, Y : MY_REAL_TYPE)
  9.                                           return MY_REAL_TYPE;
  10.    end Nested_Generic;
  11. end EasyPkg;
  12.  
  13.  
  14. package body EasyPkg is
  15.    procedure Trade_Values (X, Y : in out INTEGER) is
  16.    Temp : INTEGER;
  17.    begin
  18.       Temp := X;
  19.       X := Y;
  20.       Y := Temp;
  21.    end Trade_Values;
  22.  
  23.    package body Nested_Generic is
  24.       function Average_Values (X, Y : MY_REAL_TYPE)
  25.                                           return MY_REAL_TYPE is
  26.       begin
  27.          return (X + Y) / 2.0;
  28.       end Average_Values;
  29.    end Nested_Generic;
  30.  
  31. end EasyPkg;
  32.  
  33.  
  34.  
  35. with Text_IO, EasyPkg;
  36. use Text_IO, EasyPkg;
  37.  
  38. procedure NestPkg is
  39.  
  40. type MY_NEW_FLOAT is new FLOAT digits 6;
  41.  
  42. package Funny_Stuff is new EasyPkg.Nested_Generic(MY_NEW_FLOAT);
  43. use Funny_Stuff;
  44. package Usual_Stuff is new Nested_Generic(FLOAT);
  45. use Usual_Stuff;
  46.  
  47. Int1 : INTEGER := 12;
  48. Int2 : INTEGER := 35;
  49.  
  50. Real1 : FLOAT;
  51. My_Real1 : MY_NEW_FLOAT;
  52.  
  53. begin
  54.    Trade_Values(Int1, Int2);         -- Uses Trade_Values directly
  55.    EasyPkg.Trade_Values(Int1, Int2); -- Uses Trade_Values directly
  56. -- Usual_Stuff.Trade_Values(Int1, Int2);    -- Illegal
  57. -- Funny_Stuff.Trade_Values(Int1, Int2);    -- Illegal
  58.  
  59.    Real1 := Average_Values(2.71828, 3.141592);
  60.    Real1 := Average_Values(Real1, 2.0 * 3.141592);
  61.    My_Real1 := Average_Values(12.3, 27.345);
  62.    My_Real1 := Average_Values(My_Real1, 2.0 * 3.141592);
  63.    My_Real1 := Funny_Stuff.Average_Values(12.3, 27.345);
  64. end NestPkg;
  65.  
  66.  
  67.  
  68.  
  69. -- Result of execution
  70.  
  71. -- (There is no output from this program)
  72.  
  73.