[comp.lang.ada] Summary of "Passing procedures as parameters"

tfrancis@wpi.wpi.edu (Krishan M Nainani) (07/25/89)

Hi Netters,

	Sometime ago I wasn't quite sure how to pass procedures
as parameters in Ada and so I posted some mail to this newsgroup
and I got a variety of replies. Some people said that this was not possible
whereas others said that it was possible via generic instantiation. However,
the most detailed reply came from Mr. Arny Engelson who was very kind to 
give me the following 3 ways:

>   I do not know of an easy way to do exactly what you want (pass
>procedures and functions as parameters to a subprogram) in Ada, but
>there are several ways to do something similar to what you want.

>   First, you can pass the 'address of a subprogram as the parameter,
>and then call a system routine to invoke the subprogram.  Whether or
>not you can do this easily depends on the Ada/system_call interface
>on your system, and is not portable.

>   Second, you can convert the subprogram you want to invoke into a
>task (type), and pass a pointer to the task as the parameter.  This
>works OK, but adds some overhead and means you need to rendezvous
>with the task to pass it the necessary parameters after it is invoked.

>   Third, you can use generics.  Whether this is a viable solution
>for you depends on the particular situation you are facing.  But, you
>can both declare a generic subprogram, and also pass subprograms to
>a generic subprogram.

>   There may be other ways as well; I am not an expert in this topic.
>If you hear of any other useful ideas, please forward them (or rather
>post a summary to the net).  I would be very interested in hearing
>what else may be done, and I'm sure others would be also.

>  - Arny Engelson   att!wayback!arny

	For those who are interested, I ended up using a combination of
tasks and generic procedure passing. I had to use task types in order to
store the information in a record structure and the generic procedure
passing was used to pass the procedures to the generic package that executed
the procedures. For example, my (the user's) generic instantiation was:
	package xxx is new xyz(proc1,proc2); -- where proc1,proc2 were defined
					     --	earlier by the user
	The package xyz took the 2 procedures and, using a task type with 2 
entries (1 for each procedure), it executed each procedure within each entry
so that the accept statement for each entry only contained the procedure name.

Thanx to everyone who replied to me.

Krishan Nainani		(tfrancis@wpi.wpi.edu)