comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <laoXhai@ix.netcom.com>
Subject: Re: Self-referential types
Date: 1999/10/12
Date: 1999-10-12T11:50:42-05:00	[thread overview]
Message-ID: <7tvot2$88p@dfw-ixnews8.ix.netcom.com> (raw)
In-Reply-To: 3802f2db_2@news1.prserv.net

In article <3802f2db_2@news1.prserv.net>,
	"Matthew Heaney" <matthew_heaney@acm.org> wrote:

>Yes, of course it's possible to initialize an access object that is an
>access to a type!

Matthew is correct about this.  I have several examples of this in 
Ada book I am writing.   Here is an pared down version of one of 
them fully coded with specification, body, and a little test program.

generic
   type Item is private;
package Access_Discriminant_2 is
  type Outer is limited private;
  type Outer_Reference is access all Outer;
  procedure Initialize  (Data : in out Outer; 
                         FV : Float := 0.0;
                         IV : Integer := 0);
  function  Flag_Is     (Data : Outer) return Natural;
  function  Sentinel_Is (Data : Outer) return Float;
 private
  type Inner;
    type Inner (Outer_Link : access Outer) is limited
     record
         Inner_Value : Float := 0.0;
     end record;
  procedure Initialize (Data : in out Inner; V : Integer := 0);
  type Outer is limited
     record
        Outer_Data : Inner(Outer_Link => Outer'Access);
        Flag : Natural := 0;
     end record;
end Access_Discriminant_2;

with the following package body,

package body Access_Discriminant_2 is

  procedure Initialize (Data : in out Outer; 
                        FV : Float := 0.0;
                        IV : Integer := 0) is
  begin
     Data.Outer_Data.Inner_Value := 0.0;
     Initialize(Data.Outer_Data, V => IV);
  end Initialize;

  procedure Initialize (Data : in out Inner; V : Integer := 0) is
  begin
     Data.Outer_Link.Flag := V;
  end;

  function Flag_Is (Data : Outer) return Natural is
  begin
    return Data.Flag;
  end Flag_Is;

  function Sentinel_Is (Data : Outer) return Float
 is
  begin
    return Data.Outer_Data.Inner_Value;
  end Sentinel_Is;


end Access_Discriminant_2;

and this little program that shows how to use it,

with Access_Discriminant_2;
procedure Test_Access_Discriminant_2 is
  package Access_Demonstration is new Access_discriminant_2
                 (Item => Float);
  type Reference is access all Access_Demonstration.Outer;
  Outer_Data : aliased Access_Demonstration.Outer;
  Outer_Reference : Reference := Outer_Data'Access;
begin
  Access_Demonstration.Initialize(Outer_Reference.all, 
                                  FV =>  14.2, 
                                  IV =>  42);
end Test_Access_Discriminant_2;

This example is one of a series that demonstrates variations on the
theme, including ideas regarding improved encapsulation, and 
circumstances under which it can be appropriate in design.  

As to the notion of multiple-inheritance, most attempts to 
implement MI in Ada are no where close to true MI, including
this one.  However, this one is a pretty good composition 
mechanism for achieving other important design requirements.

Richard Riehle




  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>
1999-10-12  0:00 ` Self-referential types Robert A Duff
1999-10-12  0:00 ` Vladimir Olensky
1999-10-12  0:00   ` Matthew Heaney
1999-10-12  0:00     ` Richard D Riehle [this message]
1999-10-12  0:00     ` news.oxy.com
1999-10-12  0:00       ` Matthew Heaney
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 Ted Dennison
1999-10-12  0:00       ` Matthew Heaney
1999-10-12  0:00     ` Robert I. Eachus
1999-10-12  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
1999-10-18  0:00               ` Robert Dewar
1999-10-18  0:00                 ` Ed Falis
1999-10-19  0:00                   ` Robert Dewar
1999-10-18  0:00                 ` Brian Rogoff
     [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
     [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                     ` 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-21  0:00                     ` Jean-Pierre Rosen
1999-10-21  0:00                       ` Robert Dewar
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-13  0:00         ` Vladimir Olensky
1999-10-13  0:00           ` Vladimir Olensky
1999-10-18  0:00           ` Robert Dewar
1999-10-18  0:00             ` Laurent Guerby
1999-10-18  0:00             ` Vladimir Olensky
1999-10-13  0:00         ` Ted Dennison
1999-10-13  0:00           ` Matthew Heaney
     [not found] ` <3802597B.9205AEE8@averstar.com>
1999-10-12  0:00   ` Ted Dennison
1999-10-12  0:00     ` Matthew Heaney
1999-10-13  0:00       ` Ted Dennison
replies disabled

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