comp.lang.ada
 help / color / mirror / Atom feed
* Ada.Containers.Vectors Update_Element issue
@ 2008-05-13 17:26 Sébastien
  2008-05-13 17:55 ` Adam Beneschan
  0 siblings, 1 reply; 8+ messages in thread
From: Sébastien @ 2008-05-13 17:26 UTC (permalink / raw)


Hi,

I'm trying to use the Ada.Containers.Vectors librairies, but I don't 
know how to update element using a context.

I have a list of element and I want to use Update_Element with a 
procecedure Update_Element_Process, however I want to add static 
argument to the Update_Element_Process.

This could be something like :

procedure My_Update(c: in out context; Element: in out Element_Type);

and then

procedure Update_All(c: in out context) is
begin
	my_list.Update_Element(1, My_Update'Access);
end;

Because My_Update is not exacly the type Update_Element_Process it's nor 
working.

type Update_Element_Process is
    access procedure (Element : in out Element_Type);

Note that generic doesn't help : they give me a bug error compilation 
because of Address invalid of the generic instance.

I tried this:
generic
   c: Context;
procedure My_Update_Generic(Element: in out Element_Type);

procedure Update_All(c: in out context) is
   procedure My_Update is new My_Update_Generic(c => c);
begin
	my_list.Update_Element(1, My_Update'Access);
end;

That's not working and I can understand it ;-) No dynamical address 
since the generic will instancied at runtime or something like that ...

Thanks by advance if you have any clue to update a vector list using 
this way (because I could : get element, then update, then replace, but 
it's going to be really slower ...)

S�bastien



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

* Re: Ada.Containers.Vectors Update_Element issue
  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
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2008-05-13 17:55 UTC (permalink / raw)


On May 13, 10:26 am, Sébastien <seb.mor...@gmail.com> wrote:
> Hi,
>
> I'm trying to use the Ada.Containers.Vectors librairies, but I don't
> know how to update element using a context.
>
> I have a list of element and I want to use Update_Element with a
> procecedure Update_Element_Process, however I want to add static
> argument to the Update_Element_Process.
>
> This could be something like :
>
> procedure My_Update(c: in out context; Element: in out Element_Type);
>
> and then
>
> procedure Update_All(c: in out context) is
> begin
>         my_list.Update_Element(1, My_Update'Access);
> end;
>
> Because My_Update is not exacly the type Update_Element_Process it's nor
> working.

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.)


> type Update_Element_Process is
>     access procedure (Element : in out Element_Type);
>
> Note that generic doesn't help : they give me a bug error compilation
> because of Address invalid of the generic instance.
>
> I tried this:
> generic
>    c: Context;
> procedure My_Update_Generic(Element: in out Element_Type);
>
> procedure Update_All(c: in out context) is
>    procedure My_Update is new My_Update_Generic(c => c);
> begin
>         my_list.Update_Element(1, My_Update'Access);
> end;
>
> That's not working and I can understand it ;-) No dynamical address
> since the generic will instancied at runtime or something like that ...

Offhand, I *don't* understand why this wouldn't work.  It seems like
it should.  Is the compiler giving you an error message, or are you
getting exceptions or bad behavior at runtime?

But you shouldn't need a generic; the nested procedure solution should
work fine.

                              -- Adam





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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-13 17:55 ` Adam Beneschan
@ 2008-05-14 15:01   ` Sébastien
  2008-05-14 15:17     ` Adam Beneschan
  2008-05-17  8:13     ` Ludovic Brenta
  0 siblings, 2 replies; 8+ messages in thread
From: Sébastien @ 2008-05-14 15:01 UTC (permalink / raw)


  > 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?

Anyway it's working fine.

>> generic
>>    c: Context;
>> procedure My_Update_Generic(Element: in out Element_Type);
>>
>> procedure Update_All(c: in out context) is
>>    procedure My_Update is new My_Update_Generic(c => c);
>> begin
>>         my_list.Update_Element(1, My_Update'Access);
>> end;
>>
>> That's not working and I can understand it ;-) No dynamical address
>> since the generic will instancied at runtime or something like that ...
> 
> Offhand, I *don't* understand why this wouldn't work.  It seems like
> it should.  Is the compiler giving you an error message, or are you
> getting exceptions or bad behavior at runtime?
> 
> But you shouldn't need a generic; the nested procedure solution should
> work fine.

The compiler said it's a bug... However I'm not able to do it again, it 
works fine now but I prefer the method above.




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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-14 15:01   ` Sébastien
@ 2008-05-14 15:17     ` Adam Beneschan
  2008-05-14 18:21       ` Sébastien
  2008-05-14 21:25       ` Matthew Heaney
  2008-05-17  8:13     ` Ludovic Brenta
  1 sibling, 2 replies; 8+ messages in thread
From: Adam Beneschan @ 2008-05-14 15:17 UTC (permalink / raw)


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



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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-14 15:17     ` Adam Beneschan
@ 2008-05-14 18:21       ` Sébastien
  2008-05-14 21:25       ` Matthew Heaney
  1 sibling, 0 replies; 8+ messages in thread
From: Sébastien @ 2008-05-14 18:21 UTC (permalink / raw)


> 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.
                           -- Adam

Yes thank you very much, I got it.

Sebastien



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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-14 15:17     ` Adam Beneschan
  2008-05-14 18:21       ` Sébastien
@ 2008-05-14 21:25       ` Matthew Heaney
  2008-05-14 21:33         ` Adam Beneschan
  1 sibling, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 2008-05-14 21:25 UTC (permalink / raw)


On May 14, 11:17 am, Adam Beneschan <a...@irvine.com> wrote:
>
> 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:

No, that's not how you would do it in Ada95.  In Ada95, you would use
a generic operation:

  generic
     with procedure Process (E : in out ET);
  procedure Generic_Update (V : in out VT; I : IT);

For reasons I don't understand, many developers convert Ada05
anonymous access subprogram parameters to named access types when back-
porting to Ada95, which is completely wrong, because then you wouldn't
be able to nest the declaration of the process subprogram.



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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-14 21:25       ` Matthew Heaney
@ 2008-05-14 21:33         ` Adam Beneschan
  0 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2008-05-14 21:33 UTC (permalink / raw)


On May 14, 2:25 pm, Matthew Heaney <mhea...@on2.com> wrote:
> On May 14, 11:17 am, Adam Beneschan <a...@irvine.com> wrote:
>
>
>
> > 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:
>
> No, that's not how you would do it in Ada95.  In Ada95, you would use
> a generic operation:
>
>   generic
>      with procedure Process (E : in out ET);
>   procedure Generic_Update (V : in out VT; I : IT);
>
> For reasons I don't understand, many developers convert Ada05
> anonymous access subprogram parameters to named access types when back-
> porting to Ada95, which is completely wrong, because then you wouldn't
> be able to nest the declaration of the process subprogram.

Well, you're right, but in my defense I wasn't trying to teach the OP
how to write an Ada.Containers.Vectors package in Ada 95---I was just
trying to explain what "anonymous access-subprogram types" are in Ada
2005.

                                    -- Adam




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

* Re: Ada.Containers.Vectors Update_Element issue
  2008-05-14 15:01   ` Sébastien
  2008-05-14 15:17     ` Adam Beneschan
@ 2008-05-17  8:13     ` Ludovic Brenta
  1 sibling, 0 replies; 8+ messages in thread
From: Ludovic Brenta @ 2008-05-17  8:13 UTC (permalink / raw)


Sébastien <seb.morand@gmail.com> writes:
> There was no procedure access in previous ada version?

There was no *anonymous* access-to-procedure types in Ada 95; only
named access types like:

type Access_To_Procedure is access procedure (A, B : Integer);

And named access types are subject to "accessibility rules" (ARM
3.10.2, in particular clause 32) whereas anonymous access types are
not (and are subject to run-time checks instead).

-- 
Ludovic Brenta.



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

end of thread, other threads:[~2008-05-17  8:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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