comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@bix.com
Subject: Re: 'Read for pointers
Date: 2000/07/28
Date: 2000-07-28T00:00:00+00:00	[thread overview]
Message-ID: <Ynog5.77$Mg1.54771@news.pacbell.net> (raw)
In-Reply-To: uaef1lxlw.fsf@gsfc.nasa.gov

>The value of My_Handle is _not_ copied to Item at the procedure call.
>That only happens for "in" or "in out" parameters, not "out" parameters.

with Ada.Streams,
     Ada.Text_IO,
     Ada.Text_IO.Text_Streams;
procedure Test is
  type Instance is new Integer;

  type Handle is access all Instance;

  procedure Read
    (Stream : access Ada.Streams.Root_Stream_Type'Class;
     Item   : out    Handle
    );
  for Handle'Read use Read;

  procedure Read
    (Stream : access Ada.Streams.Root_Stream_Type'Class;
     Item   : out    Handle
    ) is
  begin
    Item.all := 2;
    Item := new Instance;
    Item.all := 3;
  end Read;

  Dummy_File : Ada.Text_IO.File_Type;

  Dummy_Stream_Access : Ada.Text_IO.Text_Streams.Stream_Access;

  P, Saved_P : Handle;

begin

  Ada.Text_IO.Open(Dummy_File, Ada.Text_IO.In_File, "dummy");
  Dummy_Stream_Access := Ada.Text_IO.Text_Streams.Stream(Dummy_File);

  P := new Instance;
  P.all := 1;
  Saved_P := P;
  Ada.Text_IO.Put_Line("P.all=" & Instance'image(P.all)
                       & " Saved_P.all=" & Instance'image(Saved_P.all));
  Ada.Text_IO.Put_Line("Handle'Read(Dummy_Stream_Access, P);");
  Handle'Read(Dummy_Stream_Access, P);
  Ada.Text_IO.Put_Line("P.all=" & Instance'image(P.all)
                       & " Saved_P.all=" & Instance'image(Saved_P.all));
  Ada.Text_IO.Close(Dummy_File);

end Test;

produces:

P.all= 1 Saved_P.all= 1
Handle'Read(Dummy_Stream_Access, P);
P.all= 3 Saved_P.all= 2

So Handle'Read not only returned a P to a new Instance with P.all=3,
but it also modified the instance previously pointed to by P,
setting Saved_P.all=2.
  Also, if, due to an error, it hadn't done
    Item := new Instance;
then in fact P would still point to the same thing as Saved_P, to
the eventual surprise of the programmer.




  reply	other threads:[~2000-07-28  0:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-26  0:00 'Read for pointers Ted Dennison
2000-07-26  0:00 ` Pat Rogers
2000-07-28  0:00   ` Stephen Leake
2000-07-28  0:00     ` tmoran [this message]
2000-07-29  0:00     ` Pat Rogers
2000-07-31  0:00       ` Stephen Leake
2000-07-31  0:00         ` Robert A Duff
2000-07-31  0:00         ` Pat Rogers
2000-07-26  0:00 ` Pat Rogers
2000-07-26  0:00   ` tmoran
2000-07-26  0:00     ` Pat Rogers
2000-07-27  0:00       ` tmoran
2000-07-26  0:00         ` Pat Rogers
2000-07-27  0:00   ` Ted Dennison
2000-07-27  0:00     ` Pat Rogers
2000-07-27  0:00       ` Ted Dennison
2000-07-27  0:00         ` Pat Rogers
2000-07-28  0:00 ` Stephen Leake
2000-07-31  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