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

* Re: Legal usage of downward closures
  2007-08-16 16:11 Legal usage of downward closures malaise
@ 2007-08-16 17:08 ` Adam Beneschan
  2007-08-27 10:52   ` malaise
  0 siblings, 1 reply; 3+ messages in thread
From: Adam Beneschan @ 2007-08-16 17:08 UTC (permalink / raw)


On Aug 16, 9:11 am, mala...@magic.fr wrote:
> 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).

The assignment statements are illegal.  Every "anonymous access type"
definition defines its own type; therefore Acc1 and Arg don't have the
same type, and neither of those types is the same as Acc2_Type.  GNAT
has a bug if it accepts them.

Furthermore, this would be illegal due to accessibility level rules:

    Acc2 := Acc_Type(Arg);

                               -- Adam




> 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;  -- ILLEGAL
>     Acc2 := Arg;  -- ILLEGAL
>   end Store;
>   procedure Call is
>   begin
>     Acc1.all;
>     Acc2.all;
>   end Call;
>
> end Pack;




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

* Re: Legal usage of downward closures
  2007-08-16 17:08 ` Adam Beneschan
@ 2007-08-27 10:52   ` malaise
  0 siblings, 0 replies; 3+ messages in thread
From: malaise @ 2007-08-27 10:52 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 583 bytes --]

On 16 août, 19:08, Adam Beneschan <a...@irvine.com> wrote:
> On Aug 16, 9:11 am, mala...@magic.fr wrote:
>
> The assignment statements are illegal.  Every "anonymous access type"
> definition defines its own type; therefore Acc1 and Arg don't have the
> same type, and neither of those types is the same as Acc2_Type.  GNAT
> has a bug if it accepts them.
>
> Furthermore, this would be illegal due to accessibility level rules:
>
>     Acc2 := Acc_Type(Arg);
>

Thank you.
This confirms my initial understanding.
I have reported the problem to ACT.

Regards




^ 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