comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@world.std.com>
Subject: Re: meaning of "current instance"
Date: 1999/11/11
Date: 1999-11-11T00:00:00+00:00	[thread overview]
Message-ID: <wcc7ljo3ld8.fsf@world.std.com> (raw)
In-Reply-To: 382b0da1_2@news1.prserv.net

"Matthew Heaney" <matthew_heaney@acm.org> 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




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

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-11  0:00 meaning of "current instance" Matthew Heaney
1999-11-11  0:00 ` Robert A Duff [this message]
1999-11-11  0:00   ` Matthew Heaney
1999-11-11  0:00     ` Tucker Taft
1999-11-15  0:00   ` Tucker Taft
1999-11-15  0:00     ` tmoran
1999-11-15  0:00       ` Tucker Taft
1999-11-11  0:00 ` tmoran
1999-11-11  0:00   ` Matthew Heaney
1999-11-12  0:00     ` tmoran
1999-11-11  0:00       ` Matthew Heaney
1999-11-12  0:00         ` tmoran
1999-11-12  0:00           ` Matthew Heaney
1999-11-12  0:00             ` tmoran
1999-11-12  0:00               ` Matthew Heaney
1999-11-13  0:00             ` Robert A Duff
1999-11-13  0:00           ` Nick Roberts
1999-11-13  0:00             ` Robert A Duff
1999-11-14  0:00               ` tmoran
1999-11-13  0:00                 ` Matthew Heaney
1999-11-15  0:00                   ` John English
1999-11-15  0:00                     ` Matthew Heaney
replies disabled

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