comp.lang.ada
 help / color / mirror / Atom feed
From: wdl39!mab@ford-wdl1.arpa  (Mark A Biggar)
Subject: Re: Passing procedures as parameters to procedures.
Date: 3 May 93 20:36:27 GMT	[thread overview]
Message-ID: <1993May3.203627.29250@wdl.loral.com> (raw)

In article <1993May3.190746.1043@ee.ubc.ca> luisl@ee.ubc.ca (luis linares-rojas
) writes:
>Subject: Procedures/functions as parameters to procedures/functions.
>Before commiting myself to any long term commitment with the Ada language,
>I've been revising its capabilities.  There is something that I have not
>found so far: how to pass a procedure (or a function) as a parameter to anothe
r
>procedure (or function).  Or, in the same spirit, how to create an array of
>procedures.  The question is: is there a way of doing this in Ada, and (if so)
>how?  I'd appreciate any help.

First Ada9x is adding access to procedure/function types so that is a
non-problem in Ada9x.

In Ada83, it is not quit so simple.  First notice that as there is no
way to dynamicly create procedures at run time, the set of procedures
(include functions here as well) that could be passed as a parameter or
used in an array is statically known at compile time.  So for passing
procedures just compile it up as a generic and locally instantiate the
generic in the local declarative region where you need to call the
parameterized routine.  For example, the usual integrate example:

generic
with function f(x: float) return float is <>;
function integrate(low: float; high: float);

function integrate(low: float; high: float) is
begin
    ...
end integrate;

function sin(x: float) return float is ...;
function cos(x: float) return float is ...;

Then to use it somewhere just instantiate it like so:

declare
    function int_sin(l: float; h: float) is new integrate(sin);
begin
    ...   -- code that call int_sin
end;

Now for the array of pointers case your only option it to write a procedure
containing a big case statement, but thats functiuonaly equivalent isn't it.
If you need to change behavior on the fly, just include all possible
callable procedures in the case statememnt and use a mapping function
call as the controlling expression for the case statement.

Suitable inline pragmas and a compiler optimizer that knows how to do goto
and sub-call chaining, and this can be just as effecient as the original array
of pointers to procedures.

--
Mark Biggar
mab@wdl1.wdl.loral.com

             reply	other threads:[~1993-05-03 20:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-05-03 20:36 Mark A Biggar [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-05-10 22:56 Passing procedures as parameters to procedures Jack Dean
1993-05-06  9:39 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!darwi
1993-05-06  7:33 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!torn!
1993-05-05 21:35 cis.ohio-state.edu!news.sei.cmu.edu!ajpo.sei.cmu.edu!progers
1993-05-05 17:39 Robert Dewar
1993-05-05 17:35 Robert Dewar
1993-05-05 16:30 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!us
1993-05-05 11:40 cis.ohio-state.edu!news.sei.cmu.edu!firth
1993-05-05  9:50 cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!howland.
1993-05-04 23:44 cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!ho
1993-05-04 22:13 John Goodsen
1993-05-04 19:39 cis.ohio-state.edu!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!m
1993-05-04 16:41 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!ira.u
1993-05-04 16:25 Mark A Biggar
1993-05-04 15:43 Tucker Taft
1993-05-04 14:29 cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!spool.mu.edu!
1993-05-04 13:59 Tucker Taft
1993-05-04 13:45 cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!howland.
1993-05-04 13:45 cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!cs.utexas.edu
1993-05-04 10:25 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!us
1993-05-03 19:07 cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!ho
replies disabled

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