comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Callback in Ada
Date: Sat, 27 Nov 2010 11:22:23 +0100
Date: 2010-11-27T11:22:23+01:00	[thread overview]
Message-ID: <87bp5boylc.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 8lc2d0Fb6jU1@mid.individual.net

Georg Maubach writes:
> Hi All,
>
> today I learnt what a callback function is and also how it is used in 
> Python. I have tested the following code which runs in Python:
>
> def apply_to (function, valueList):
>    resultList = []
>
>    if __debug__: print "Calling function ", function
>    
>    for value in valueList:
>       if __debug__: print "value: ", value
>       r = function (value)
>       if __debug__: print "result: ", r
>       resultList.append(r)
>
>    return resultList

This function "apply_to" is a classic example of a "passive iterator".

This example is not type-safe; apply_to assumes that function takes
exactly one argument and returns exactly one result.  What happens if
you pass a function that takes zero or two arguments? Or if it does not
return anything?

> How would I do that in Ada?

There are several ways, most of them type-safe.

Statically compiled, see ARM 12.1(24) for this example:

     generic
        type Item   is private;
        type Vector is array (Positive range <>) of Item;
        with function Sum(X, Y : Item) return Item;
     package On_Vectors is
        function Sum  (A, B : Vector) return Vector;
        function Sigma(A    : Vector) return Item;
        Length_Error : exception;
     end On_Vectors;

The body is in 12.2 and an example instantiation in 12.3(25).

The Ada containers library provides several examples of dynamically
bound callbacks, e.g. in Ada.Containers.Vectors (A.18.2(74/2)):

        procedure  Iterate
          (Container : in Vector;
           Process   : not null access procedure (Position : in Cursor));

The above is another classic example of a passive iterator.

In both of these cases, the compiler enforces type safety between the
elements in the vector, the arguments passed to the function, etc.  In
the static (generic) case, the compiler makes it impossible to call the
iterator without a callback; in the dynamic case, the compiler cannot
always check at compile time, so it inserts a run-time check.

A third, type-unsafe way to pass callbacks, is to use addresses, e.g:

type Array_Of_Items is array (Positive range <>) of Item;
procedure For_Each_Element (Of_Array : in Array_Of_Items;
                            Action   : in System.Address) is
   procedure Act (On_Item : in out Item; At_Index : in Positive);
   for Act'Address use Action;
begin
   for J in Of_Array'Range loop
      Act (On_Item => On_Array (J), At_Index => J);
   end loop;
end For_Each_Element;

Apart from passive iterators, callbacks are heavily used in GUI
programming.  You can find many examples of callbacks in the
"slot/signal" model of GtkAda.

There are alternatives to callbacks; for example I believe Claw and Qt
use overriding primitive operations instead of callbacks.

-- 
Ludovic Brenta.



  parent reply	other threads:[~2010-11-27 10:22 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 [this message]
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
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