comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!us
Subject: Re: Passing procedures as parameters to procedures.
Date: 4 May 93 10:25:18 GMT	[thread overview]
Message-ID: <1s5gae$kr4@huon.itd.adelaide.edu.au> (raw)

>From article <1993May3.203627.29250@wdl.loral.com>, by mab@wdl39.wdl.loral.com
 (Mark A Biggar):
> In article <1993May3.190746.1043@ee.ubc.ca> luisl@ee.ubc.ca (luis linares-roj
as) 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 anoth
er
>>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
)

Any good Ada book (e.g. Barnes' "Programming in Ada") would reveal
that you cannot do this directly in Ada83.

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

Yes, and very welcome, too. If any of the 9X heavies can explain to me
why this has to be done in terms of access types, though, I'd be
grateful. The fact that this is done in terms of a procedure closure
is an implementation detail, it seems to me. Why not just have
something like:

type some_proc is procedure;

.
.
.

some_proc := a_procedure;

Access types in 9X are becoming a bit too overloaded for my liking.
(I'll live with it, though!)


> 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:
> 
[example omitted]
> 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 arra
y
> of pointers to procedures.
> 


As I pointed out in this group about a year ago, there are some things
that you want to be able to do that you just can't with generics. If
anybody can provide me with a good, clean, Ada equivalent of the
following Pascal code, I'll send him/her (first winner only!) a good
bottle of Australian red wine (damn this Beaujolais business!).

procedure a(b : procedure(c : procedure));

  procedure d;	
  begin;
  end;

 begin
   b(d);
 end;


procedure x(y : procedure);
begin
  y;
end;

.
.
.
a(x);
.
.


(Of course, I've abstracted away the inessential details).

In Ada9X the answer will look like this:

type d_proc is access procedure;
type b_proc is access procedure(c : d_proc);

procedure a(b : b_proc) is
  procedure d is
  begin
    null;
  end;
begin
  b(d'access);
end;

procedure x(y : d_proc) is
begin
  y.all;
end;
.
.
.
a(x'access);
.
.
.

BTW, this example comes from real, working and useful Pascal code, not
(as some might think) from a contest on obfuscated Pascal!


Happy programming.
#  Andrew Dunstan                   #   There's nothing good or bad   #
#  net:                             #                                 #
#    adunstan@steptoe.adl.csa.oz.au #   but thinking makes it so.     #
#  or: andrewd@cs.adelaide.edu.au   #                                 #

             reply	other threads:[~1993-05-04 10:25 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-05-04 10:25 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!us [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-03 20:36 Mark A Biggar
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