comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Re-exporting primitive operations of a private type (like "-", and 'min)
Date: Sun, 30 Oct 2005 09:57:27 +0100
Date: 2005-10-30T09:57:25+01:00	[thread overview]
Message-ID: <1v9k0ezbwyz6o.1dyyxzvm1mezv.dlg@40tude.net> (raw)
In-Reply-To: slrndm8143.52o.bogus_addy@tango.mindfuq.org

On Sat, 29 Oct 2005 21:19:27 GMT, Anonymous Coward wrote:

> I'm bothered by the fact that the primitive operations of a parent are
> not inherited in a private type.

They are but in the view where inheritance happens. In your example it
happens in the private view.

> In particular, the basic arithmetic
> operations are lost, like "+", "-", 'min, 'max, etc.

"+' and "-" are rather hidden.

> There seems to
> be no clean way to re-export these operations,

Yep. That's because the original operation cannot be named. Otherwise, one
could just rename new "-" to the old one. As for attributes, it is another
case, they cannot be user-defined at all.

> Redefining "-" is an annoyance.

In general, the problem is that old-fashioned way emerged before Ada became
OO. Private_Distance_Type is a numeric type. That should be its *public*
contract. But in Ada it is impossible to specify things like this for
non-tagged types [outside generics, to be precise.] The compiler cannot
recognize the programmer's intent from a bunch of operations declared in
the public part. So it does not. Therefore when in the private part
Private_Distance_Type gets implemented as a clone of a numeric type, the
compiler treats public "-" as a different operation declared.

[ I'm not sure if interfaces of Ada 2005 might help here. Let's wait for a
working compiler. ]

Anyway, you could go this way:

   ...
private
   type Most_Private_Distance_Type is new Public_Distance_Type;
   function Sub (Left, Right : in Most_Private_Distance_Type)
      return Most_Private_Distance_Type renames "-";
       -- Renaming "as-declaration" creates a new primitive operation Sub

   type Private_Distance_Type is new Most_Private_Distance_Type;
       -- Both "-" and Sub are inherited, but "-" gets hidden by "-"
       -- declared in the public part

   function "-" (Left, Right : in Private_Distance_Type)
      return Private_Distance_Type renames Sub;
       -- Renaming "as-body" implements public "-" through private "-"
       -- via Sub

Warning, this may crash GNAT 3.15p which has many bugs in visibility rules
(let language lawyers correct me if I'm wrong and the above is illegal.) I
didn't try such things with GNAT GPL but it is even more buggy. (:-()

> I'm not even sure how to redefine
> attribute operations like 'min.  Suggestions?

They aren't "normal" operations, as they should be, so forget about them.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2005-10-30  8:57 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 [this message]
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
replies disabled

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