comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm-host.bauhaus@maps.futureapps.de>
Subject: Re: Callback in Ada
Date: Sat, 27 Nov 2010 12:32:54 +0100
Date: 2010-11-27T12:32:56+01:00	[thread overview]
Message-ID: <4cf0ec67$0$6882$9b4e6d93@newsspool2.arcor-online.net> (raw)
In-Reply-To: <8lc2d0Fb6jU1@mid.individual.net>

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.



  parent reply	other threads:[~2010-11-27 11:32 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-27  9:47 Callback in Ada Georg Maubach
2010-11-27 10:11 ` Dmitry A. Kazakov
2010-11-27 10:22 ` Ludovic Brenta
2010-11-27 11:04   ` Vinzent Hoefler
2010-11-27 22:05   ` Maciej Sobczak
2010-11-27 10:26 ` Alex Mentis
2010-11-27 10:46 ` Vinzent Hoefler
2010-11-27 11:32 ` Georg Bauhaus [this message]
2010-11-27 21:11   ` Jeffrey Carter
2010-11-27 21:20     ` Dmitry A. Kazakov
2010-11-28 21:35       ` Maciej Sobczak
2010-11-29  8:41         ` Dmitry A. Kazakov
2010-11-29 10:12           ` Maciej Sobczak
2010-11-29 11:04             ` Dmitry A. Kazakov
2010-11-30  1:32               ` Randy Brukardt
2010-11-30  8:38                 ` Dmitry A. Kazakov
2010-11-30  9:14                 ` AdaMagica
2010-11-30 12:37                   ` Georg Bauhaus
2010-11-30 20:28                     ` Randy Brukardt
2010-11-30 20:31                   ` Randy Brukardt
2011-01-01 16:46                 ` Yannick Duchêne (Hibou57)
2011-01-02 10:14                   ` AdaMagica
2011-01-02 14:11                     ` Robert A Duff
2011-01-02 15:15                       ` AdaMagica
2011-01-02 15:28                         ` Robert A Duff
2011-01-02 15:38                       ` Alex Mentis
2011-01-02 15:44                         ` Robert A Duff
2011-01-03 21:38                           ` Randy Brukardt
2011-01-04  0:11                             ` Robert A Duff
2011-01-04 18:33                             ` Alex Mentis
2011-01-04 19:47                               ` Robert A Duff
2011-01-04 20:21                                 ` Randy Brukardt
2011-01-04 20:15                               ` Randy Brukardt
2010-11-29 15:24     ` Callback in Ada (User Data argument) Warren
2010-11-30  1:35       ` Randy Brukardt
2010-11-30 16:51         ` Warren
replies disabled

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