comp.lang.ada
 help / color / mirror / Atom feed
* Legal usage of downward closures
@ 2007-08-16 16:11 malaise
  2007-08-16 17:08 ` Adam Beneschan
  0 siblings, 1 reply; 3+ messages in thread
From: malaise @ 2007-08-16 16:11 UTC (permalink / raw)


Hi,

I have problems understanding the RAT05 section on downward closures.

I thought that anonymous access to a sub-program, as parameter of a
sub-program, would prevent local copies and further usage out of
scope.
Surprisingly, the following example compiles an runs (on GNAT GPL
2007).

package Pack is
  procedure Store (Arg : access procedure);
  procedure Call;
end Pack;

package body Pack is
  Acc1 : access procedure;
  type Acc_Type is access procedure;
  Acc2 : Acc_Type;

  procedure Store (Arg : access procedure) is
  begin
    Acc1 := Arg;
    Acc2 := Arg;
  end Store;
  procedure Call is
  begin
    Acc1.all;
    Acc2.all;
  end Call;

end Pack;
--------------------------------------
with Ada.Text_Io;
with Pack;
procedure Tes is
begin
  declare
    I : Integer := 21;
    procedure Proc is
    begin
      Ada.Text_Io.Put_Line ("In Proc" & I'Img);
    end Proc;
  begin
    Pack.Store (Proc'Access);
  end;

  declare
    I : Integer := 22;
  begin
    Pack.Call;
  end;
end Tes;

This produces the output:
In Proc 21
In Proc 21
despite my attempt to "overwrite" 21 by 22 :-)


So it seems that the feature is rather to guarantee persistence of the
referenced sub-program and its scope as long as a reference to it is
stored somewhere.
Is it so much "magic"?

How long are Proc and I=21 kept in memory for further calls to "Call".
Are they cleared if I call Pack.Store with an access to another
procedure?

Thank's




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-08-27 10:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-16 16:11 Legal usage of downward closures malaise
2007-08-16 17:08 ` Adam Beneschan
2007-08-27 10:52   ` malaise

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