comp.lang.ada
 help / color / mirror / Atom feed
From: jgv@swl.msd.ray.com (John Volan)
Subject: Re: Mut. Recurs. in Ada9X w/o Breaking Encaps.? (LONG)
Date: Thu, 29 Sep 1994 02:12:09 GMT
Date: 1994-09-29T02:12:09+00:00	[thread overview]
Message-ID: <1994Sep29.021209.20769@swlvx2.msd.ray.com> (raw)
In-Reply-To: 36bt0c$17oo@watnews1.watson.ibm.com

NHC = ncohen@watson.ibm.com (Norman H. Cohen) writes:

NHC>One variation on this is to declare recursive types meant to serve as the
NHC>parents of types Employee and Office, but to provide no operations for
NHC>these recursive types.  Then, in child packages, declare Employee and
NHC>Office themselves as derived types and declare primitive operations in
NHC>those child packages: 
NHC>
NHC>    with ... ; -- other stuff needed by Employee
NHC>    with ... ; -- other stuff needed by Office
NHC>
NHC>    package Employees_And_Offices is
NHC>
NHC>      type Employee_Parent is tagged limited private;
NHC>      type Employee_Pointer is access all Employee'Class;
NHC>
NHC>      type Office_Parent is tagged limited private;
NHC>      type Office_Pointer is access all Office'Class;

This solution suffers from the same problem as Mark Biggar's suggestion:
These access types only give clients an "opaque" view of the designated objects.
The useful primitive operations for these objects won't be declared until we 
get to the concrete *subclasses* declared later.  So a client would have to
resort to using a non-typesafe Unchecked_Conversion to "downcast" one of these
pointers into designating the corresponding concrete subclass.

Another issue is whether the concrete subclasses declared below are going to
be the *only* subclasses of these abstract classes.  Certainly that's the
original intent -- but will a maintainer pay any attention? ;-)

NHC>    private
NHC>
NHC>      type Employee_Parent is tagged
NHC>        record
NHC>          ... -- various components involving that "other stuff"
NHC>          Its_Occupied_Office : Office_Pointer;
NHC>          ...
NHC>        end record;
NHC>
NHC>      type Office_Parent is tagged
NHC>        record
NHC>          ... -- various components involving that "other stuff"
NHC>          Its_Occupying_Employee : Employee_Pointer;
NHC>          ...
NHC>        end record;
NHC>
NHC>    end Employees_And_Offices;

Another problem here is that this doesn't really solve the original puzzle I
posed:  How do you avoid breaking encapsulation *between* these two classes?
These type declarations are private, but since the packages below are all
children of this parent package, they have complete visibility to the private
part of the parent.  So an Employee subprogram still has license to ignore
the public primitives of the Office class and "diddle" with the Office's
private components directly.  (And vice versa.)

NHC>    package Employees_And_Offices.Employees is
NHC>       type Employee is tagged private;
NHC>       No_Employee : constant Employee_Pointer := null;
NHC>      ... -- Employee subprograms involving that "other stuff"
NHC>    private
NHC>       type Employee is new Employee_Parent with null record;
NHC>    end Employees_And_Offices.Employees;
NHC>
NHC>    package Employees_And_Offices.Offices is
NHC>       type Office is tagged private;
NHC>       No_Office : constant Office_Pointer := null;
NHC>      ... -- Office subprograms involving that "other stuff"
NHC>    private
NHC>       type Office is new Office_Parent with null record;
NHC>    end Employees_And_Offices.Offices;
NHC>
NHC>
NHC>    with Employees_And_Offices.Employees, Employees_And_Offices.Offices;
NHC>
NHC>    package Employees_And_Offices.Common is
NHC>
NHC>      function Office_Occupied_By (The_Employee : in Employee)
NHC>        return Office_Pointer;
NHC>
NHC>      function Employee_Occupying (The_Office : in Office)
NHC>        return Employee_Pointer;
NHC>
NHC>      procedure Occupy (The_Office   : in Office_Pointer;
NHC>                        The_Employee : in Employee_Pointer);
NHC>
NHC>    end Employees_And_Offices.Common;
NHC>
NHC>The operations in the Common child are not primitive operations of either
NHC>type.  (Two types cannot possibly both have primitive operations with
NHC>parameters of the other type, because the declarations of the packages
NHC>declaring those types would have to name each other in with clauses.  If
NHC>you don't mind breaking the symmetry, you can make the Offices child
NHC>depend on the Employees child and put ALL the common operations in the
NHC>Offices child as primitive operations of type Office, or vice versa.)
NHC>The bodies of all three child packages have complete visibility into the
NHC>mutually recursive data structure declared in Employees_And_Offices.  Since
NHC>Office_Pointer is declared to designate Office_Parent'Class rather than
NHC>Office_Parent, there is no problem with Office_Pointer values designating
NHC>Office objects, and similarly for the Employee-related types.

Ah, but there *is* a problem: An Office_Pointer does't manifestly designate an
*Office* object.  Rather, it designates an *Office_Parent view* of what *might*
be an Office object.  This view, being "opaque", is not useful to a client.

NHC>
NHC>--
NHC>Norman H. Cohen    ncohen@watson.ibm.com

Well, this doesn't really work, but I think we're starting to get closer ...
Thanks anyway!

-- John Volan

--------------------------------------------------------------------------------
--  Me : Person := (Name                => "John Volan",
--                  Company             => "Raytheon Missile Systems Division",
--                  E_Mail_Address      => "jgv@swl.msd.ray.com",
--                  Affiliation         => "Enthusiastic member of Team Ada!",
--                  Humorous_Disclaimer => "These opinions are undefined " &
--                                         "by my employer and therefore " &
--                                         "any use of them would be "     &
--                                         "totally erroneous.");
--------------------------------------------------------------------------------



  reply	other threads:[~1994-09-29  2:12 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
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 [this message]
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