comp.lang.ada
 help / color / mirror / Atom feed
* Re-exporting primitive operations of a private type (like "-", and 'min)
@ 2005-10-29 21:19 Anonymous Coward
  2005-10-30  8:57 ` Dmitry A. Kazakov
  2005-10-30  9:18 ` Jeffrey R. Carter
  0 siblings, 2 replies; 17+ messages in thread
From: Anonymous Coward @ 2005-10-29 21:19 UTC (permalink / raw)


I'm bothered by the fact that the primitive operations of a parent are
not inherited in a private type.  In particular, the basic arithmetic
operations are lost, like "+", "-", 'min, 'max, etc.  There seems to
be no clean way to re-export these operations, or am I missing
something?  Here is some sample code:

[distance.ads]

   package Distance is

      type Public_Distance_Type is new Float;

      procedure Some_Procedure 
        (Sample_Variable : in Public_Distance_Type);


      type Private_Distance_Type is private;

      Zero_Distance  : constant Private_Distance_Type;
      Large_Distance : constant Private_Distance_Type;

      function "-" (Left, Right : in Private_Distance_Type)
        return Private_Distance_Type;

   private

      type Private_Distance_Type is new Public_Distance_Type;

      Zero_Distance  : constant Private_Distance_Type := 0.0;
      Large_Distance : constant Private_Distance_Type := 999_999_999.0;

   end Distance;

[distance.adb]

   package body Distance is

      function "-" (Left, Right : in Private_Distance_Type)
        return Private_Distance_Type is

      begin

         --This is ugly.  Isn't there a way to declare this
         --operation without redefining it?  The type casting
         --is also a nuissance, but the compiler warns of
         --infinite loops if casting is not used.

         return Private_Distance_Type(Public_Distance_Type(Left) -
                                      Public_Distance_Type(Right));

      end "-";

      procedure Some_Procedure (Sample_Variable : in Public_Distance_Type)is
      begin

         --This was another simple test to see whether we can
         --inherit explicitly declared public operations.

         null;

      end Some_Procedure;

   end Distance;

[use_distance.adb]

   with Distance;

   procedure Use_Distance is

      Public_Distance  : Distance.Public_Distance_Type;
      Private_Distance : Distance.Private_Distance_Type;

      use type Distance.Private_Distance_Type;
      use type Distance.Public_Distance_Type;

   begin

      Public_Distance := Distance.Public_Distance_Type'Min(-5.0,7.0);

      --The following line fails to compile because
      --'min is not inherited.
      --
      --Private_Distance := Distance.Private_Distance_Type'Min
      --  (Distance.Zero_Distance,Distance.Large_Distance);

      Distance.Some_Procedure (Sample_Variable => Public_Distance);

      --The following line fails to compile because
      --Some_Procedure is not inherited.
      --
      --Distance.Some_Procedure (Sample_Variable => Private_Distance);

      --This line compiles because it was redefined explicitly
      --
      Private_Distance := Distance.Large_Distance -
                          Distance.Zero_Distance;

   end Use_Distance;

---

Redefining "-" is an annoyance.  I'm not even sure how to redefine
attribute operations like 'min.  Suggestions?



^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2005-11-03  9:15 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-29 21:19 Re-exporting primitive operations of a private type (like "-", and 'min) Anonymous Coward
2005-10-30  8:57 ` Dmitry A. Kazakov
2005-10-30  9:18 ` Jeffrey R. Carter
2005-10-30 14:16   ` Martin Krischik
2005-10-30 22:35     ` Robert A Duff
2005-10-31  4:15   ` Anonymous Coward
2005-10-31  4:34     ` Anonymous Coward
2005-10-31  6:14     ` Jeffrey R. Carter
2005-11-01  3:39       ` Anonymous Coward
2005-11-01  4:47         ` Jeffrey R. Carter
2005-11-01 23:43           ` Anonymous Coward
2005-11-02  3:35             ` Jeffrey R. Carter
2005-11-01 14:11         ` Robert A Duff
2005-10-31  8:27     ` Niklas Holsti
2005-10-31  9:18       ` Dmitry A. Kazakov
2005-11-02  9:50       ` Jean-Pierre Rosen
2005-11-03  9:15         ` Niklas Holsti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox