comp.lang.ada
 help / color / mirror / Atom feed
From: Anonymous Coward <bogus_addy@bogus_domain.net>
Subject: Re: Re-exporting primitive operations of a private type (like "-", and 'min)
Date: Mon, 31 Oct 2005 04:15:06 GMT
Date: 2005-10-31T04:15:06+00:00	[thread overview]
Message-ID: <slrndmbdiv.4s0.bogus_addy@tango.mindfuq.org> (raw)
In-Reply-To: Cd09f.1502$8c5.566@newsread3.news.pas.earthlink.net

> The whole point of private types is that the only visible operations
> on them are assignment, equality and inequality, and any explicitly
> defined operations in the visible part of the package
> specification. 

That makes sense.  As a default, it probably would be a poor language
design to have private types automatically export public primitive
operations.  If I created an Altitude_Type, I wouldn't want to inherit
the "+" operation.  However, I was hoping for a one liner (or a
pragma) that would enable me to explicitly declare operations for
public use.  The fact that these must be redefined really seems like a
short-coming of the language.

Anyway, it's not the end of the world.  I mainly just wanted to get an
idea for cleanest way to handle this dirty situation.. and I like the
suggestions you folks made.  I appreciate all your advice.  I haven't
decided which approach I like better, but here's what my sample code
looks like right now:

[distance.ads]

   package Distance is

      type Public_Distance_Type is new Float;

      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;

      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

      -- Implementation operations ------------------------------------------------

      function Subtract (Left, Right : in Private_Distance_Type)
        return Private_Distance_Type is

      begin

         --Kazakov's suggested approach.  This is good because it's
         --simple, and doesn't require type conversions.  
         --(May not work with gnat 3.15p)

         return (Left - Right);

      end Subtract;

      -- Public operations --------------------------------------------------------

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


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

      begin

         --Krischik's suggested approach.  This is better than what I
         --had started with because I don't need to reference the
         --parent type explicitly.

         return Private_Distance_Type(Private_Distance_Type'Base(Left) -
                                      Private_Distance_Type'Base(Right));

      end "+";

   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

      --This operation is forever invisible in the
      --Private_Distance_Type, and can only be declared as an
      --operation with a new name.
      --
      Public_Distance := Distance.Public_Distance_Type'Min(-5.0,7.0);

      --These lines compile only because they are redefined
        explicitly, using function overloading.
      --
      Private_Distance := Distance.Large_Distance -
                          Distance.Zero_Distance;

      Private_Distance := Distance.Large_Distance +
                          Distance.Zero_Distance;

   end Use_Distance;

---

In my real life case, there is no Public_Distance_Type.  It's really
just derived from a Float, but I use it here for clarity and testing
purposes.  



  parent reply	other threads:[~2005-10-31  4:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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