From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e5bfd51af02edca2 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller.gnilink.net!gnilink.net!trnddc02.POSTED!20ae255c!not-for-mail Newsgroups: comp.lang.ada From: Anonymous Coward Subject: Re: Re-exporting primitive operations of a private type (like "-", and 'min) References: Message-Id: User-Agent: slrn/0.9.7.4 (Linux) Date: Mon, 31 Oct 2005 04:15:06 GMT NNTP-Posting-Host: 141.149.78.234 X-Complaints-To: abuse@verizon.net X-Trace: trnddc02 1130732106 141.149.78.234 (Sun, 30 Oct 2005 23:15:06 EST) NNTP-Posting-Date: Sun, 30 Oct 2005 23:15:06 EST Xref: g2news1.google.com comp.lang.ada:6063 Date: 2005-10-31T04:15:06+00:00 List-Id: > 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.