TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Ludovic Brenta <[log in to unmask]>
Thu, 11 Nov 2004 14:33:44 +0100
text/plain (49 lines)
Roger Racine writes:
> At 01:04 PM 11/10/2004 -0800, Prichard, Jayson (Space Technology) wrote:
>>I am writing a subroutine that has one parameter, another subroutine that
>>I want the second subroutine to execute.  Does anyone have any ideas how
>>to do this?
> Ada95 or Ada83?  With Ada95 it is quite straightforward (see section 3.10
> of the Reference Manual).  You simply create an access type and use the
> access type  as the parameter.
> ...
> type procedure_access is access procedure (. . .);
> procedure my_proc (proc_access : in procedure_access);
> ...
> procedure real_proc (...)  -- same arguments as in the type declaration
> ...
> my_proc (real_proc'access);
> For Ada83, it could still be done for all the compilers I used, but was
> implementation dependent.  As I recall, one used the 'address of the
> procedure, and then used a representation clause to call the procedure.
> Roger Racine

Another way is to use a generic:

procedure Test is
      with procedure To_Be_Called (The_Parameter : in Whatever);
   procedure Generic_Proc;

   procedure Generic_Proc is
      To_Be_Called (The_Parameter => ...);
   end Generic_Proc;

   procedure First (The_Parameter : in Whatever) is separate;

   procedure Second is new Generic_Proc (To_Be_Called => Second);
begin -- Test
end Test;

Generics are usually preferred to access-to-subprograms because they
are safer since no null access value can ever exist.

Ludovic Brenta.