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,87557ce53b069315 X-Google-Attributes: gid103376,public From: Robert A Duff Subject: Re: meaning of "current instance" Date: 1999/11/11 Message-ID: #1/1 X-Deja-AN: 547429611 Sender: bobduff@world.std.com (Robert A Duff) References: <382b0da1_2@news1.prserv.net> Organization: The World Public Access UNIX, Brookline, MA Newsgroups: comp.lang.ada Date: 1999-11-11T00:00:00+00:00 List-Id: "Matthew Heaney" writes: > I have some code below that I thought had a bug (um, I mean "error"), ;-) > but now I'm not so sure. > > Suppose I have this: > > type T is tagged private; > ... > function Init (O : access T'Class) > > type T is > tagged record > I : Integer := Init (T'Access); > end record; I think you want T to be limited, or else use 'Unchecked_Access. > If I have an object of type T: > > declare > O1 : T; > begin > > then it's clear that the "current instance" to which T'Access refers has > type T. > > But now suppose I derive from T: > > type NT is new T with null record; > > and declare an instance of type NT: > > declare > O2 : NT; > begin > > Object O2 (like O1) has a component I, which is initialized with the > "current instance." But what is the type of the "current instance" > here, passed to Init? Um, I guess it's T, but what difference does it make? It's *tag* is NT'Tag. Inside Init, we have a value of an anonymous access-to-class-wide. So if Init dispatches, it will dispatch to an NT operation. > --STX > with Ada.Text_IO; use Ada.Text_IO; > > package body P.C is > > function Do_Init (O : access NT) return Integer is > begin > Put_Line ("P.C.Do_Init"); > return 1; > end; > > end P.C; > package P.C is > > type NT is new T with null record; > > private > > function Do_Init (O : access NT) return Integer; > > end P.C; > with Ada.Text_IO; use Ada.Text_IO; > > package body P is > > procedure Put (O : in T'Class) is > begin > Put_Line ("I is" & Integer'Image (O.I)); > end; > > function Init (O : access T'Class) return Integer is > begin > Put_Line ("P.Init"); > return Do_Init (O); > end; > > > function Do_Init (O : access T) return Integer is > begin > Put_Line ("P.Do_Init"); > return 0; > end; > > > end P; > package P is > > type T is tagged limited private; > > procedure Put (O : in T'Class); > > private > > function Init (O : access T'Class) return Integer; > > function Do_Init (O : access T) return Integer; > > type T is > tagged limited record > I : Integer := Init (T'Access); > end record; > > end P; > with P; use P; > with P.C; use P.C; > > procedure Test_P is > OT : T; > ON : NT; > begin > Put (OT); > Put (ON); > end; Seems to me it should print: I is 0 I is 1 But I don't see what the type of the expression T'Access has to do with it, so perhaps I'm misunderstanding your question. - Bob