From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 4 May 93 10:25:18 GMT From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!us enet.ins.cwru.edu!agate!dog.ee.lbl.gov!network.ucsd.edu!munnari.oz.au!yoyo.aarn et.edu.au!news.adelaide.edu.au! (Andrew Dunstan,,2285592,) Subject: Re: Passing procedures as parameters to procedures. Message-ID: <1s5gae$kr4@huon.itd.adelaide.edu.au> List-Id: >>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 # #