comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Self-referential types
Date: 1999/10/12
Date: 1999-10-12T00:00:00+00:00	[thread overview]
Message-ID: <3803c8bc_2@news1.prserv.net> (raw)
In-Reply-To: 3803B5E3.F96A6DD4@mitre.org

In article <3803B5E3.F96A6DD4@mitre.org> , "Robert I. Eachus" 
<eachus@mitre.org> wrote:

>> This is the basis for programming with access discriminants, which is
>> how you do MI in Ada95.
>
>    I have to butt in here.  It may be how you do MI in Ada, and it is
> one way, but I find it ugly.  If you treat a private access type as the
> object, then you have to do a bit more work inside the package, but the
> exterior looks much cleaner, and the users don't have to use 'Access at
> all.  (Often you don't need it inside the package either.)

I think we're in agreement.

I use access discriminants (indeed, taggedness) strictly as an
implementation technique.  As much as possible you want to push the type
composition infrastructure into the private part of the spec.

For example, most of my observer types only privately derive from
Observer_Type; the public part is just "limited private."

with Clock_Timers;
package Digital_Clocks is

  type Digital_Clock (Timer : access Clock_Timer'Class) is
    limited private;

private

  type Control_Type (Clock : access Digital_Clock) is
    new Limited_Controlled with null record;

  type Digital_Clock (Timer : access Clock_Timer'Class) is
    new Observer_Type (Timer) with record
      Control : Control_Type (Digital_Clock'Access);  --!!!
    end record;

end Digital_Clocks;

Here, Digital_Clock "multiply inherits" from two different parent types.
All the machinery is hidden in the private region, exactly where it
belongs.


I do one better when I observe two subjects simultaneously:

with Master_Timers;
with Slave_Timers;
package Digital_Clocks is

  type Digital_Clock
    (Master : access Master_Timer;
     Slave  : access Slave_Timer) is limited private;

private

  type Master_Obs_Type (Clock : access Digital_Clock) is
    new Observer_Type with null record;

  type Slave_Obs_Type (Clock : access Digital_Clock) is
    new Observer_Type with null record;

  type Control_Type (Clock : access Digital_Clock) is
    new Limited_Controlled with null record;

  type Digital_Clock (...) is
    limited record
      Master_Obs : Master_Obs_Type (Digital_Clock'Access);
      Slave_Obs  : Slave_Obs_Type (Digital_Clock'Access);
      Control    : Control_Type (Digital_Clock'Access);
    end record;

end Digital_Clocks;


Here, Digital_Clock "multiply inherits" from three different parent
types.  But all the machinery is hidden in the private region, exactly
where it should be.

In recent article I showed how to observe individual attributes of a
subject, as compared to observing the subject itself.

If you declare the observer types as children of the subject, then you
can even hide the fact that the subject derives from Subject_Type.

Some guys use public taggedness everywhere with gay abandon.  This is
wrong.  Don't use taggedness unless you need to, and if you do, then
hide it in the private region if you can.

Under no circumstances should you have deep inheritance hierarchies.
Make hierarchies broad and shallow; say, an abstract parent type and a
non-abstract sibling derivations.  (This is the approach I use over and
over again in my articles in the Design Patterns archive.)

I realize this is not the OO approach of other languages, but the
benefits are that you have far less coupling.

These examples (and many more) can all be found in the design patterns
archive.

<http://www.acm.org/archives/patterns.html>


I will be discussing this technique this Sunday (17 Oct 99) during my
Design Patterns tutorial, at this year's SIGAda conference.

Matt


--
The political forces that try to eliminate evolution from science
classrooms impose a narrow, sectarian doctrine on our educational
systems. This imposition represents an affront not only to the
constitutional separation of church and state but also to the moral and
intellectual integrity embedded in that constitution.

<http://www.nabt.org/evolutionks.html>




  reply	other threads:[~1999-10-12  0:00 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <7ttb4a$8mq$1@nnrp1.deja.com>
     [not found] ` <3802597B.9205AEE8@averstar.com>
1999-10-12  0:00   ` Self-referential types Ted Dennison
1999-10-12  0:00     ` Matthew Heaney
1999-10-13  0:00       ` Ted Dennison
1999-10-12  0:00 ` Vladimir Olensky
1999-10-12  0:00   ` Matthew Heaney
1999-10-12  0:00     ` news.oxy.com
1999-10-12  0:00       ` Ted Dennison
1999-10-12  0:00         ` Stanley R. Allen
1999-10-13  0:00           ` Ted Dennison
1999-10-13  0:00         ` Vladimir Olensky
1999-10-14  0:00         ` Multiple Inheritance in Ada 95 [was Re: Self-referential types] Tucker Taft
1999-10-12  0:00       ` Self-referential types Matthew Heaney
1999-10-12  0:00     ` Richard D Riehle
1999-10-12  0:00     ` Robert I. Eachus
1999-10-12  0:00       ` Matthew Heaney [this message]
1999-10-13  0:00         ` Vladimir Olensky
1999-10-13  0:00           ` Vladimir Olensky
1999-10-18  0:00           ` Robert Dewar
1999-10-18  0:00             ` Vladimir Olensky
1999-10-18  0:00             ` Laurent Guerby
1999-10-13  0:00         ` Ted Dennison
1999-10-13  0:00           ` Matthew Heaney
1999-10-13  0:00         ` Robert I. Eachus
1999-10-13  0:00           ` Brian Rogoff
1999-10-15  0:00             ` Robert I. Eachus
1999-10-15  0:00               ` Marin David Condic
1999-10-15  0:00                 ` Robert I. Eachus
1999-10-18  0:00                   ` Robert Dewar
1999-10-19  0:00                     ` Robert I. Eachus
     [not found]               ` <7u86su$o5v$1@nntp8.atl.mindspring.net>
1999-10-18  0:00                 ` Robert I. Eachus
1999-10-22  0:00                   ` Richard D Riehle
1999-10-22  0:00                     ` Robert I. Eachus
1999-10-18  0:00               ` Robert Dewar
1999-10-18  0:00                 ` Brian Rogoff
1999-10-18  0:00                 ` Ed Falis
1999-10-19  0:00                   ` Robert Dewar
     [not found]               ` <slrn80fl9f.68j.aidan@skinner.demon.co.uk>
1999-10-19  0:00                 ` Wes Groleau
1999-10-21  0:00                   ` Robert Dewar
1999-10-21  0:00                     ` Jean-Pierre Rosen
1999-10-21  0:00                       ` Robert Dewar
1999-10-21  0:00                     ` Comments (was: Self-referential types) Wes Groleau
1999-10-21  0:00                       ` Ehud Lamm
1999-10-22  0:00                         ` Ted Dennison
1999-10-23  0:00                           ` Ehud Lamm
1999-10-23  0:00                         ` Robert Dewar
1999-10-23  0:00                           ` Ehud Lamm
1999-10-23  0:00                             ` Comments Georg Bauhaus
1999-10-24  0:00                               ` Comments Ehud Lamm
1999-10-26  0:00                                 ` Comments Robert I. Eachus
1999-10-28  0:00                                   ` Comments Jerry van Dijk
1999-10-28  0:00                                     ` Comments Ted Dennison
1999-10-25  0:00                             ` Comments (was: Self-referential types) Wes Groleau
1999-10-23  0:00                       ` M.
     [not found]                       ` <Pine.A41.3.96-heb-2.07.991021191504.30582K-100000@pluto.mscc.huji. <381477c9.e1388ff3@ftw.rsc.raytheon.com>
1999-10-25  0:00                         ` Larry Kilgallen
1999-10-21  0:00                     ` Self-referential types Larry Kilgallen
1999-10-22  0:00                     ` Richard D Riehle
1999-10-23  0:00                       ` Robert A Duff
1999-10-23  0:00                         ` Richard D Riehle
1999-10-24  0:00                       ` Michel DELARCHE
1999-10-12  0:00     ` Ted Dennison
1999-10-12  0:00       ` Matthew Heaney
1999-10-12  0:00 ` Robert A Duff
replies disabled

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