comp.lang.ada
 help / color / mirror / Atom feed
From: stt@dsd.camb.inmet.com (Tucker Taft)
Subject: Re: Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG)
Date: Thu, 29 Sep 1994 13:57:56 GMT
Date: 1994-09-29T13:57:56+00:00	[thread overview]
Message-ID: <Cww9GL.EI@inmet.camb.inmet.com> (raw)
In-Reply-To: 1994Sep29.014611.20263@swlvx2.msd.ray.com

In article <1994Sep29.014611.20263@swlvx2.msd.ray.com>,
John Volan <jgv@swl.msd.ray.com> wrote:

> ...
>Let's say some client of Employee asks an Employee object for its Office.  The
>Employee object will give back one of these "opaque" pointers in response.  What
>can the client do with that pointer?  As far as it can see, the Office object
>being pointed to is totally featureless.  Oh, sure, it's possible that this
>Office object is really some subclass of Abstract_Office that does have useful
>features, but this information isn't available to the client.  The only way the
>client could get to that information would be to use an unchecked conversion to 
>change this "opaque" pointer into a pointer to the "non-opaque" Office subclass
>below.  (I think folks in the other languages would call this "downcasting".)
>But this practice breaks type-safety!

Actually, you can "down cast" (aka "narrow") safely in Ada 9X.
There is no need to revert to unchecked conversion.
If you have (a pointer to) a class-wide type, you can explicitly convert
it to (a pointer to) any type "covered" by the class-wide type.
A run-time check is performed as appropriate to make sure what you are
doing is copacetic.  See RM9X-4.6(15, 23, 42, 50);5.0.

For example:

     type T is abstract tagged private;
     type PT is access all T'Class;

   ...

     type T1 is new T with private;
     type PT1 is access all T1'Class;

   ...

     Y : PT;
     X : PT1 := PT1(Y);  -- Run-time check performed, as appropriate

    ...

     function To_T1(A : T'Class) return T1'Class is
     begin
         return T1'Class(A);  -- Run-time check performed, as appropriate
     end To_T1;

This capability of Ada 9X is vaguely related to the "assignment attempt"
of Eiffel, and the dynamic_cast of ANSI/ISO C++-to-be, but manages
to fit quite nicely into the existing Ada 83 concept of (safe) explicit
conversion.

Note that Ada 9X also has a way to check whether a given object
is in a give class before attempting a conversion, as a generalization
of the Ada 83 concept of membership test:

    if A in T1'Class then ...  -- Checks whether "tag" of A indicates
                               -- that it is in class of types rooted at T1.

    if Y = null or else Y.all in T1'Class then ...
           -- Checks that PT1(Y) will succeed, before
	   -- attempting it.

So using access-to-root-abstract-class-wide-type is a viable and safe
approach in Ada 9X, particularly when you "know" there is only
one direct derivative of the root abstract type, and you are
converting to that. 

However, putting two mutually recursive types in the same
package is the traditional Ada way of solving this problem, and
seems preferable to me.  You can preserve modularity by
making these types abstract, while eliminating the need for
explicit type conversions by declaring all of the interesting 
mutually-recursive primitives in this one package.

S. Tucker Taft    stt@inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA 02138



  reply	other threads:[~1994-09-29 13:57 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-09-27 16:52 Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG) John Volan
1994-09-27 18:48 ` Mark A Biggar
1994-09-29  1:46   ` John Volan
1994-09-29 13:57     ` Tucker Taft [this message]
1994-09-29 17:20       ` Bjarne Stroustrup <9758-26353> 0112760
1994-09-30  1:38         ` Tucker Taft
1994-09-30 12:33           ` Bjarne Stroustrup <9758-26353> 0112760
1994-09-29 18:37       ` John Volan
1994-09-29 19:34         ` David Weller
1994-09-30 22:13           ` John Volan
1994-10-02  3:31             ` Andrew Lees
1994-09-30  1:47         ` Tucker Taft
1994-09-30 13:30           ` John Volan
1994-09-29 18:10     ` R. William Beckwith
1994-10-03  0:33     ` Cyrille Comar
1994-09-28 14:01 ` Norman H. Cohen
1994-09-29  2:12   ` John Volan
1994-09-29 14:01     ` Tucker Taft
1994-09-29 18:37     ` Norman H. Cohen
1994-09-29  9:48   ` Magnus Kempe
1994-09-29 13:10     ` Magnus Kempe
1994-09-29 18:05       ` Tucker Taft
1994-09-30 10:20         ` Mut. Recurs. in Ada9X w/o Breaking Encaps.? Magnus Kempe
1994-09-30 13:22           ` Tucker Taft
1994-10-01  1:24       ` Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG) Adam Beneschan
1994-10-01 12:01         ` Magnus Kempe
1994-10-01 18:43         ` Mark A Biggar
1994-10-02 16:41         ` John Volan
1994-10-02 23:33           ` Matt Kennel
1994-10-03  8:07           ` Mut. Recurs. in Ada9X w/o Breaking Encaps.? Magnus Kempe
1994-10-03 12:14           ` Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG) Robert I. Eachus
1994-10-04  2:12             ` R. William Beckwith
1994-10-04 16:00             ` John Volan
1994-10-05 11:42               ` Robert I. Eachus
1994-10-05 21:09               ` Matt Kennel
1994-10-03 20:29           ` Harry Koehnemann
1994-09-29 13:35     ` John Volan
1994-09-30 20:27       ` Norman H. Cohen
1994-10-01  1:47         ` John Volan
1994-10-01 20:44           ` Tucker Taft
1994-10-03 11:29           ` Robert I. Eachus
1994-09-30 22:46       ` Matt Kennel
1994-10-01  2:11         ` John Volan
replies disabled

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