comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Ada.Containers.Vectors Update_Element issue
Date: Wed, 14 May 2008 08:17:36 -0700 (PDT)
Date: 2008-05-14T08:17:36-07:00	[thread overview]
Message-ID: <8d5f96c4-9a65-4e99-bc1b-a281a547e213@l28g2000prd.googlegroups.com> (raw)
In-Reply-To: 482AFEAC.3040306@gmail.com

On May 14, 8:01 am, Sébastien <seb.mor...@gmail.com> wrote:
>   > procedure Update_All (c : in out context) is
>
> >     procedure Do_The_Update (Element : in out Element_Type) is
> >     begin
> >         My_Update (c, Element);
> >     end Do_Update;
> > begin
> >     my_list.Update_Element (1, Do_The_Update'Access);
> > end Update_All;
>
> > I haven't tried this.  However, it should work because the Process
> > parameter to Update_Element is declared as an anonymous access-
> > procedure type, rather than as a named access-procedure type, and this
> > means you can pass a nested procedure access to it without any
> > accessibility-level issues.  (This sort of usage is exactly why
> > anonymous access-subprograms types were added to Ada 2005.)
>
> Ok I didn't think about nested proc having access to argument. When you
> mean anonymous access, you are meaning "Do_The_Update'Access"? There was
> no procedure access in previous ada version?

What I mean is that the procedure is declared with an anonymous access
subprogram parameter.  Here's the declaration of Update_Element in the
Vectors package:

   procedure Update_Element
     (Container : in out Vector;
      Index     : in     Index_Type;
      Process   : not null access procedure
                      (Element : in out Element_Type));

The declaration of Process is "not null access procedure".  This is an
*anonymous* access because the access type isn't given a name.  In Ada
95, you would have to make this a named access type:

   type Process_Type is access procedure (Element : in out
Element_Type);
        -- "not null" didn't exist in Ada 95
   procedure Update_Element
     (Container : in out Vector;
      Index     : in     Index_Type;
      Process   : in     Process_Type);

You can still do this in Ada 2005, but if you do, you would only be
allowed to pass Proc'Access as a parameter if Proc was a *global*
procedure (not nested inside any other procedure).  The reason is that
Update_Element could save Process in some global variable.  Sometimes
that's what you want---you want to be able to save a procedure access
and use it later.  But in the actual declaration, the Process
parameter is of an anonymous access type, which means that you can
pass an 'Access of any procedure, no matter how nested it is, but
Update_Element can't save the procedure access in a variable
anywhere.  Hope that explains things.

> Anyway it's working fine.

Great!

                                -- Adam



  reply	other threads:[~2008-05-14 15:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-13 17:26 Ada.Containers.Vectors Update_Element issue Sébastien
2008-05-13 17:55 ` Adam Beneschan
2008-05-14 15:01   ` Sébastien
2008-05-14 15:17     ` Adam Beneschan [this message]
2008-05-14 18:21       ` Sébastien
2008-05-14 21:25       ` Matthew Heaney
2008-05-14 21:33         ` Adam Beneschan
2008-05-17  8:13     ` Ludovic Brenta
replies disabled

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