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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e94a7e4f6f888766 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Self-referential types Date: 1999/10/12 Message-ID: <7tvot2$88p@dfw-ixnews8.ix.netcom.com>#1/1 X-Deja-AN: 535873091 References: <7ttb4a$8mq$1@nnrp1.deja.com> <3802f2db_2@news1.prserv.net> Organization: Netcom X-NETCOM-Date: Tue Oct 12 11:50:42 AM CDT 1999 Newsgroups: comp.lang.ada Date: 1999-10-12T11:50:42-05:00 List-Id: In article <3802f2db_2@news1.prserv.net>, "Matthew Heaney" 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