From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,de7dd126d6737f3a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder.news-service.com!kanaga.switch.ch!news-zh.switch.ch!switch.ch!news.belwue.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Sat, 27 Nov 2010 12:32:54 +0100 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101027 Thunderbird/3.1.6 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Callback in Ada References: <8lc2d0Fb6jU1@mid.individual.net> In-Reply-To: <8lc2d0Fb6jU1@mid.individual.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4cf0ec67$0$6882$9b4e6d93@newsspool2.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 27 Nov 2010 12:32:56 CET NNTP-Posting-Host: 9c303060.newsspool2.arcor-online.net X-Trace: DXC=lH8EkT0Un63n`gW2MTm]<3A9EHlD;3Yc24Fo<]lROoR18kFW@4PCY\c7>ejV8ba3faFPBI32R4;37Ye=ND9 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:16649 Date: 2010-11-27T12:32:56+01:00 List-Id: On 11/27/10 10:47 AM, Georg Maubach wrote: > Hi All, > > today I learnt what a callback function is and also how it is used in > Python. ... > How would I do that in Ada? You have a number of choices. 1. Generics, since Ada 83 generic with function ... -- to be applied procedure Apply_To_Each (Els : in out Some_Seq_Type); generic with function ... function Map_Concat (Els : in out Some_Seq_Type) return Some_Seq_Type; 2. Function pointers type To_Be_Applied is access function ( ... ); (Or the Ada 2005 anonymous variant if you really need to avoid names.) function Map_Concat (Els: in out Some_Seq_Type; f : To_Be_Applied ) return Some_Seq_Type; 3. Do what Python does when an object implements the __call__ interface: A callable object in Python is a callable object in Ada if the primitive operations of that Ada object's type includes a __call__-like subprogram. That is, one that has a profile similar to that of Python's __call__. type Callable is interface; function Call (Me : access Callable; Arg : T) return T is abstract; -- or "in out Callable" in Ada 201X type My_Callable is ... and Callable; overriding function Call (Me : access My_Callable; Arg : T) return T; A difference between Ada and Python is that in 1 and 2 above, the function thing can be passed in only, whereas Python functions can return functions and procedures to be assigned to, for example, global variables. I don't think this can be fully achieved in Ada, because then a "function object" would have a life time longer than its (local) type. For a more involved example, see Chris Okasaki's http://okasaki.blogspot.com/2008/07/functional-programming-inada.html I vaguely remember a short outline on lambda-the-ultimate.org.