From: malaise@magic.fr
Subject: Legal usage of downward closures
Date: Thu, 16 Aug 2007 09:11:18 -0700
Date: 2007-08-16T09:11:18-07:00 [thread overview]
Message-ID: <1187280678.505830.294720@a39g2000hsc.googlegroups.com> (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
next reply other threads:[~2007-08-16 16:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-08-16 16:11 malaise [this message]
2007-08-16 17:08 ` Legal usage of downward closures Adam Beneschan
2007-08-27 10:52 ` malaise
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox