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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,66253344eaef63db X-Google-Attributes: gid109fba,public X-Google-Thread: 1108a1,66253344eaef63db X-Google-Attributes: gid1108a1,public X-Google-Thread: 103376,66253344eaef63db X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-09-29 19:40:28 PST Newsgroups: comp.lang.ada,comp.object,comp.lang.c++ Path: bga.com!news.sprintlink.net!howland.reston.ans.net!gatech!newsxfer.itd.umich.edu!zip.eecs.umich.edu!yeshua.marcam.com!MathWorks.Com!noc.near.net!inmet!dsd!stt From: stt@dsd.camb.inmet.com (Tucker Taft) Subject: Re: Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG) Message-ID: Keywords: Ada 9X, C++, object-oriented Sender: news@inmet.camb.inmet.com Organization: Intermetrics, Inc. References: <1994Sep27.165203.9192@swlvx2.msd.ray.com> <1994Sep27.184827.17813@wdl.loral.com> <1994Sep29.014611.20263@swlvx2.msd.ray.com> Date: Thu, 29 Sep 1994 13:57:56 GMT Xref: bga.com comp.lang.ada:6320 comp.object:6902 comp.lang.c++:31025 Date: 1994-09-29T13:57:56+00:00 List-Id: In article <1994Sep29.014611.20263@swlvx2.msd.ray.com>, John Volan 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