comp.lang.ada
 help / color / mirror / Atom feed
* Callback in Ada
@ 2010-11-27  9:47 Georg Maubach
  2010-11-27 10:11 ` Dmitry A. Kazakov
                   ` (4 more replies)
  0 siblings, 5 replies; 36+ messages in thread
From: Georg Maubach @ 2010-11-27  9:47 UTC (permalink / raw)


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

def dublicate (value):
   return (value * 2)

def power (value):
   return (value * value)

valueList = [1, 2, 3, 4, 5]

double_values = apply_to(dublicate, valueList)
power_values = apply_to(power, valueList)

print "Values doubled: ", double_values
print "Values powered: ", power_values

Reference: http://en.wikipedia.org/wiki/Callback_(computer_science)

If put into a file called "callback-test-in-python.py" and called
python -O callback-test-in-python.py it will deliver the output

Values doubled: 2, 4, 6, 8, 10
Values powered: 1, 4, 9, 16, 25

How would I do that in Ada?

Regards

George




^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 36+ messages in thread
From: Dmitry A. Kazakov @ 2010-11-27 10:11 UTC (permalink / raw)


On 27 Nov 2010 09:47:12 GMT, Georg Maubach wrote:

> How would I do that in Ada?

   type Integer_Array is array (Positive range <>) of Integer;
   procedure Apply
             (  Action : not null access procedure
                          (Value : in out Integer);
                To : in out Integer_Array
             )  is
   begin
      for Index in To'Range loop
         Action (To (Index));
      end loop;
   end Apply;
   
   procedure Double (Value : in out Integer) is
   begin
      Value := Value * 2;
   end Double;

Used as:

   List : Integer_Array := (1,2,3,4,5);
begin
   Apply (Double'Access, List);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 36+ messages in thread
From: Ludovic Brenta @ 2010-11-27 10:22 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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 10:26 ` Alex Mentis
  2010-11-27 10:46 ` Vinzent Hoefler
  2010-11-27 11:32 ` Georg Bauhaus
  4 siblings, 0 replies; 36+ messages in thread
From: Alex Mentis @ 2010-11-27 10:26 UTC (permalink / raw)


Georg Maubach wrote:

> 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
> 
> def dublicate (value):
>    return (value * 2)
> 
> def power (value):
>    return (value * value)
> 
> valueList = [1, 2, 3, 4, 5]
> 
> double_values = apply_to(dublicate, valueList)
> power_values = apply_to(power, valueList)
> 
> print "Values doubled: ", double_values
> print "Values powered: ", power_values
> 
> Reference: http://en.wikipedia.org/wiki/Callback_(computer_science)
> 
> If put into a file called "callback-test-in-python.py" and called
> python -O callback-test-in-python.py it will deliver the output
> 
> Values doubled: 2, 4, 6, 8, 10
> Values powered: 1, 4, 9, 16, 25
> 
> How would I do that in Ada?
> 
> Regards
> 
> George

Basically the same idea as Dimitry, except I use functions so the code
works more similarly to your Python:

with Ada.Text_IO, Ada.Integer_Text_IO;
use  Ada.Text_IO, Ada.Integer_Text_IO;

procedure Callback is
   
   type Int_Array is array (1..5) of Integer;
   
   function Apply_To 
     (Func       : not null access function (Value : Integer) return
Integer;
      Value_List : Int_Array) return Int_Array is            
      
      Result : Int_Array;
      
   begin -- Apply_To
      for Value in Value_List'Range loop
         Result (Value) := Func (Value_List(Value));
      end loop;
      return Result;
   end Apply_To;       
   
   function  Dublicate (Value : Integer) return Integer is
   begin -- Dublicate
      return Value * 2;
   end Dublicate;
   
   Initial : Int_Array := (1, 2, 3, 4, 5);
   Result : Int_Array;
   
begin
   
   Result := Apply_To (Dublicate'Access, Initial);
   
   Put("Values doubled: ");
   for Value in Result'Range loop
      Put (Result (Value), 0);
      if Value /= Result'Last then
         Put (", ");
      end if;
   end loop;
   
end Callback;



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27  9:47 Callback in Ada Georg Maubach
                   ` (2 preceding siblings ...)
  2010-11-27 10:26 ` Alex Mentis
@ 2010-11-27 10:46 ` Vinzent Hoefler
  2010-11-27 11:32 ` Georg Bauhaus
  4 siblings, 0 replies; 36+ messages in thread
From: Vinzent Hoefler @ 2010-11-27 10:46 UTC (permalink / raw)


Georg Maubach wrote:

> 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):
[...]

> Values doubled: 2, 4, 6, 8, 10
> Values powered: 1, 4, 9, 16, 25
>
> How would I do that in Ada?

The important thing is the Callback type and the 'Access attribute for the
function which you like to call.

-- 8< --
with Ada.Text_IO;

procedure Callback is

     function Duplicate (Value : in Integer) return Integer is
     begin
        return Value * 2;
     end Duplicate;

     function Power (Value : in Integer) return Integer is
     begin
        return Value * Value;
     end Power;

     type Int_List is array (Positive range <>) of Integer;

     type Callback is access function (Value : in Integer) return Integer;

     procedure Apply_To (Operation : Callback; Values : Int_List) is
        R : Integer;
     begin
        for V in Values'Range
        loop
           R := Operation (Values (V));
           Ada.Text_IO.Put (Integer'Image (R));
        end loop;

        Ada.Text_IO.New_Line;
     end Apply_To;

     Value_List : constant Int_List := (1, 2, 3, 4, 5);

begin
     Apply_To (Duplicate'Access, Value_List);
     Apply_To (Power'Access,     Value_List);
end Callback;
-- 8< --

Vinzent.



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27 10:22 ` Ludovic Brenta
@ 2010-11-27 11:04   ` Vinzent Hoefler
  2010-11-27 22:05   ` Maciej Sobczak
  1 sibling, 0 replies; 36+ messages in thread
From: Vinzent Hoefler @ 2010-11-27 11:04 UTC (permalink / raw)


Ludovic Brenta wrote:

> 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?

Then the interpreter tells you something along the lines

"function expects exactly one argument, but two given."

> Or if it does not return anything?

Then it will be of the "None"-type. This is Python, not Perl. ;)

Of course, just like in every dynamically typed language, it crashes
at run-time, not at compile-time.


Vinzent.

-- 
Beaten by the odds since 1974.



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27  9:47 Callback in Ada Georg Maubach
                   ` (3 preceding siblings ...)
  2010-11-27 10:46 ` Vinzent Hoefler
@ 2010-11-27 11:32 ` Georg Bauhaus
  2010-11-27 21:11   ` Jeffrey Carter
  4 siblings, 1 reply; 36+ messages in thread
From: Georg Bauhaus @ 2010-11-27 11:32 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27 11:32 ` Georg Bauhaus
@ 2010-11-27 21:11   ` Jeffrey Carter
  2010-11-27 21:20     ` Dmitry A. Kazakov
  2010-11-29 15:24     ` Callback in Ada (User Data argument) Warren
  0 siblings, 2 replies; 36+ messages in thread
From: Jeffrey Carter @ 2010-11-27 21:11 UTC (permalink / raw)


On 11/27/2010 04:32 AM, Georg Bauhaus wrote:
>
> 2. Function pointers
>
> type To_Be_Applied is access function ( ... );
> (Or the Ada 2005 anonymous variant if you really need to
> avoid names.)

While anonymous access types in current Ada seem designed to allow the developer 
to avoid creating type names and to adopt a C-like coding style, in the case of 
access-to-subprogram, the anonymous form has an advantage over named types: they 
allow passing access to a local subprogram ("downward funargs") without any of 
the accessibility checks or worries that accompany named types. I can't say I'm 
a fan of the syntax, but that's a useful capability.

-- 
Jeff Carter
"There's no messiah here. There's a mess all right, but no messiah."
Monty Python's Life of Brian
84



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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 15:24     ` Callback in Ada (User Data argument) Warren
  1 sibling, 1 reply; 36+ messages in thread
From: Dmitry A. Kazakov @ 2010-11-27 21:20 UTC (permalink / raw)


On Sat, 27 Nov 2010 14:11:36 -0700, Jeffrey Carter wrote:

> in the case of 
> access-to-subprogram, the anonymous form has an advantage over named types: they 
> allow passing access to a local subprogram ("downward funargs") without any of 
> the accessibility checks or worries that accompany named types. I can't say I'm 
> a fan of the syntax, but that's a useful capability.

Nevertheless it is a language design flaw. The language should have had
procedural types, i.e. instead of:

   procedure Foo (Closure : not null access procedure (I : Integer));
   procedure Bar (I : Integer);
   ...
   Foo (Bar'Access);

we would be able to write:

   procedure Foo (Closure : procedure (I : Integer));
   procedure Bar (I : Integer);
   ...
   Foo (Bar);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27 10:22 ` Ludovic Brenta
  2010-11-27 11:04   ` Vinzent Hoefler
@ 2010-11-27 22:05   ` Maciej Sobczak
  1 sibling, 0 replies; 36+ messages in thread
From: Maciej Sobczak @ 2010-11-27 22:05 UTC (permalink / raw)


On 27 Lis, 11:22, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:

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

YAMI4 uses this approach for delivering messages and various
notifications.
AWS uses this, too.

You have left that at the end of the list as if it was the least
capable option, but I think that callbacks implemented this way (in
particular with interfaces) have several important advantages:

1. They can be stateful. Procedure callbacks can refer to enclosing
state as well, but for example having an array of stateful callbacks
is less obvious. Interfaces can do that.

2. They are composable. A single entity can implement many callbacks
and this can be enforced at the level of type system, not by mere
convention.

3. Last but certainly not least: they can be implemented directly as
protected or task objects.

I would not use them for the passive iterator idiom, but for any form
of asynchronous delivery, they can be very powerful.

--
Maciej Sobczak * http://www.inspirel.com



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-27 21:20     ` Dmitry A. Kazakov
@ 2010-11-28 21:35       ` Maciej Sobczak
  2010-11-29  8:41         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 36+ messages in thread
From: Maciej Sobczak @ 2010-11-28 21:35 UTC (permalink / raw)


On 27 Lis, 22:20, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:

> we would be able to write:
>
>    procedure Foo (Closure : procedure (I : Integer));
>    procedure Bar (I : Integer);
>    ...
>    Foo (Bar);

And now an equivalent example with functions, please. :-)

--
Maciej Sobczak * http://www.inspirel.com



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-28 21:35       ` Maciej Sobczak
@ 2010-11-29  8:41         ` Dmitry A. Kazakov
  2010-11-29 10:12           ` Maciej Sobczak
  0 siblings, 1 reply; 36+ messages in thread
From: Dmitry A. Kazakov @ 2010-11-29  8:41 UTC (permalink / raw)


On Sun, 28 Nov 2010 13:35:12 -0800 (PST), Maciej Sobczak wrote:

> On 27 Lis, 22:20, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
> 
>> we would be able to write:
>>
>> � �procedure Foo (Closure : procedure (I : Integer));
>> � �procedure Bar (I : Integer);
>> � �...
>> � �Foo (Bar);
> 
> And now an equivalent example with functions, please. :-)

 � �procedure Foo (Closure : function return Integer);
 � �function Bar return Integer;
 � �...
 � �Foo (Bar);

There is no difference.

You probably think that there might be a problem in resolving Bar in Foo,
because Bar could mean a call to Bar. But this is no problem in Ada [*].
Foo expects a function argument, it does not expect Integer.

---------------
* Ada is not bottom up as C++, it can resolve using the type of the result.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-29  8:41         ` Dmitry A. Kazakov
@ 2010-11-29 10:12           ` Maciej Sobczak
  2010-11-29 11:04             ` Dmitry A. Kazakov
  0 siblings, 1 reply; 36+ messages in thread
From: Maciej Sobczak @ 2010-11-29 10:12 UTC (permalink / raw)


On Nov 29, 9:41 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:

> > And now an equivalent example with functions, please. :-)
>
>     procedure Foo (Closure : function return Integer);
>     function Bar return Integer;
>     ...
>     Foo (Bar);
>
> There is no difference.

There is.

> * Ada is not bottom up as C++, it can resolve using the type of the result.

But here there is no type of the result. Bar is not even called, so
there is no result on which you could disambiguate this construct.
In order to make your reasoning complete you would have to introduce
additional way of using function names:

1. This way:

   Bar

would mean that the function is called. The type of result can be used
to disambiguate the overloaded calls. For the brave: watch out
enumerations.

2. This way:

   Bar

would mean that the function is *not* called, but is denoted as a
first-class entity.

Oops, they look the same. You have to disambiguate them. And once you
do, you have to disambiguate *again*, if it happens to be 1. above.
This would mean two-phase overload disambiguation. Don't even try to
combine these two phases, that would be complete mess.

Probably it would be better to add some attribute (like in qualified
expressions) to hint the compiler whether you intend to use the
function as an entity or to actually call it - but then, you gain
absolutely nothing with regard to the existing 'Access attribute,
which you consider as being broken.

In any case, this:

   Foo (Bar);

would add yet another ambiguity to what is already too heavily
overloaded syntax. Hardly readable at all.

--
Maciej Sobczak * http://www.inspirel.com



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-29 10:12           ` Maciej Sobczak
@ 2010-11-29 11:04             ` Dmitry A. Kazakov
  2010-11-30  1:32               ` Randy Brukardt
  0 siblings, 1 reply; 36+ messages in thread
From: Dmitry A. Kazakov @ 2010-11-29 11:04 UTC (permalink / raw)


On Mon, 29 Nov 2010 02:12:33 -0800 (PST), Maciej Sobczak wrote:

> On Nov 29, 9:41�am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
> 
>>> And now an equivalent example with functions, please. :-)
>>
>> �� �procedure Foo (Closure : function return Integer);
>> �� �function Bar return Integer;
>> �� �...
>> �� �Foo (Bar);
>>
>> There is no difference.
> 
> There is.
> 
>> * Ada is not bottom up as C++, it can resolve using the type of the result.
> 
> But here there is no type of the result.

Of course there is. The result type is an anonymous procedural type:

   type <anonymous> is function return Integer;

> Bar is not even called, so
> there is no result on which you could disambiguate this construct.

Yes, it is an object. BTW, the following is legal Ada:

   package P is
      type X is (V);
   end P;
   package Q is
      function V return Float;
   end Q;
   package body Q is
      function V return Float is
      begin
         return 3.14;
      end V;
   end Q;
   use P, Q;
   X : Float := V; -- Resolved between P.V and Q.V

> In order to make your reasoning complete you would have to introduce
> additional way of using function names:
> 
> 1. This way:
> 
>    Bar
> 
> would mean that the function is called. The type of result can be used
> to disambiguate the overloaded calls. For the brave: watch out
> enumerations.
> 
> 2. This way:
> 
>    Bar
> 
> would mean that the function is *not* called, but is denoted as a
> first-class entity.

Yes and both ways already exist in Ada, how otherwise would you be able to
take an access to the subprogram:

   Bar'Access

reads #2: use the object Bar and apply 'Access to it. It does not read #1:
call Bar and apply 'Access to the result.

Once you introduce subprograms as first-class entities in any form (even
through pointers), you get the ambiguity. The same happens with referential
types, e.g. Ptr as self vs. Ptr as target. There is nothing to do about it.
Whatever magic words you would put around Ptr it would never be clear if
these apply to Ptr or to Ptr.all, without further premises.

The right answer is resolution according to the type. In a typed system the
context tells the meaning of the operation. No problem.

P.S. In fact Ada had this kind of stuff from the day one. In Ada 83 there
were no pointers to subprograms, but it had tasks. We customary used tasks
where a program needed as a parameter. Nobody died.

P.P.S. As Ada strives to introduce procedural-literals, e.g. conditional
expressions, fancy quantified expressions and other awful stuff, it will
inevitably require subprogram types, because you cannot get pointer of a
literal. What is all this mess worth of if it cannot be given as a
parameter?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada (User Data argument)
  2010-11-27 21:11   ` Jeffrey Carter
  2010-11-27 21:20     ` Dmitry A. Kazakov
@ 2010-11-29 15:24     ` Warren
  2010-11-30  1:35       ` Randy Brukardt
  1 sibling, 1 reply; 36+ messages in thread
From: Warren @ 2010-11-29 15:24 UTC (permalink / raw)


Jeffrey Carter expounded in
news:icrshe$lt2$1@tornado.tornevall.net: 

> On 11/27/2010 04:32 AM, Georg Bauhaus wrote:
>>
>> 2. Function pointers
>>
>> type To_Be_Applied is access function ( ... );
>> (Or the Ada 2005 anonymous variant if you really need to
>> avoid names.)
> 
> While anonymous access types in current Ada seem designed
> to allow the developer to avoid creating type names and to
> adopt a C-like coding style, in the case of 
> access-to-subprogram, the anonymous form has an advantage
> over named types: they allow passing access to a local
> subprogram ("downward funargs") without any of the
> accessibility checks or worries that accompany named types.

The one area that I've struggled with in callback design is 
the "user data" argument, which is often included in callbacks 
for context (XWindow functions are a good example).  The user 
data argument gives the callback the "context" or "object" for 
which the event is for.

The problem with the "user data" argument, is that you don't 
know what the user's type is going to be (in C, this is given 
as a void *). The Ada dirty approach might be to use 
'System_Address, but that seems wrong. Still another approach 
is to use a generic.

Perhaps the best avenue might be to declare a base tagged 
type, that the user can extend. Then the user data argument 
could be the base classwide type.

Warren



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-29 11:04             ` Dmitry A. Kazakov
@ 2010-11-30  1:32               ` Randy Brukardt
  2010-11-30  8:38                 ` Dmitry A. Kazakov
                                   ` (2 more replies)
  0 siblings, 3 replies; 36+ messages in thread
From: Randy Brukardt @ 2010-11-30  1:32 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:10p6vzd761rdz$.1gtmc0fk6t9rb.dlg@40tude.net...
...
> P.P.S. As Ada strives to introduce procedural-literals, e.g. conditional
> expressions, fancy quantified expressions and other awful stuff, it will
> inevitably require subprogram types, because you cannot get pointer of a
> literal. What is all this mess worth of if it cannot be given as a
> parameter?

No problem here, just use an expression function to give your expression a 
name (and parameters); then you can use 'Access just fine. Forget anonymous 
stuff, it only leads to heartache. :-)

    function Foobar (A : Integer) return Integer is (if A > 10 then A - 5 
else A);

 [The only exception is anonymous access-to-subprogram, and that is because 
of the special closure semantics. It would have been better to use a keyword 
to get that semantics, something like:
    type Local_Sub is local access function (A : Integer) return Integer;
but this isn't sufficiently broken for such a change.]

                                        Randy.

                              Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada (User Data argument)
  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
  0 siblings, 1 reply; 36+ messages in thread
From: Randy Brukardt @ 2010-11-30  1:35 UTC (permalink / raw)


"Warren" <ve3wwg@gmail.com> wrote in message 
news:Xns9E3F69D4E88CDWarrensBlatherings@188.40.43.230...
...
> The problem with the "user data" argument, is that you don't
> know what the user's type is going to be (in C, this is given
> as a void *). The Ada dirty approach might be to use
> 'System_Address, but that seems wrong. Still another approach
> is to use a generic.
>
> Perhaps the best avenue might be to declare a base tagged
> type, that the user can extend. Then the user data argument
> could be the base classwide type.

That is the best avenue, unless the "user data" type can be (or needs to be) 
part of the "root" tagged object. For instance, in Claw, we used the "user 
data" type in order to provide a link between the Windows handle and the 
Claw object. That's OK in that if you need private "user data", you can just 
add it to your own extension of the Claw data type.

                             Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-30  1:32               ` Randy Brukardt
@ 2010-11-30  8:38                 ` Dmitry A. Kazakov
  2010-11-30  9:14                 ` AdaMagica
  2011-01-01 16:46                 ` Yannick Duchêne (Hibou57)
  2 siblings, 0 replies; 36+ messages in thread
From: Dmitry A. Kazakov @ 2010-11-30  8:38 UTC (permalink / raw)


On Mon, 29 Nov 2010 19:32:55 -0600, Randy Brukardt wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
> news:10p6vzd761rdz$.1gtmc0fk6t9rb.dlg@40tude.net...
> ...
>> P.P.S. As Ada strives to introduce procedural-literals, e.g. conditional
>> expressions, fancy quantified expressions and other awful stuff, it will
>> inevitably require subprogram types, because you cannot get pointer of a
>> literal. What is all this mess worth of if it cannot be given as a
>> parameter?
> 
> No problem here, just use an expression function to give your expression a 
> name (and parameters); then you can use 'Access just fine. Forget anonymous 
> stuff, it only leads to heartache. :-)

Agree.

BTW, this is another problem with access to subprogram types. Though the
type might be explicit the type of the object (subprogram) is implicit.
When passing a name in many cases you get a cryptic messages, because the
anonymous type is matched against the access type. In presence of
overloaded generic instances it becomes a nightmare. This is the case in
GtkAda signal handlers, for example. If there were procedural types, things
would be explicit:

   type Call_Back is procedure (...)
   procedure Do_Something (Action : Call_Back);
   ...

   My_Call_Back : constant Call_Back := -- I know this will always match
   begin 
      ...
   end My_Call_Back;

The syntax must be improved, but the idea is clear: My_Call_Back is
explicitly declared of being of the Call_Back type.

>  [The only exception is anonymous access-to-subprogram, and that is because 
> of the special closure semantics. It would have been better to use a keyword 
> to get that semantics, something like:
>     type Local_Sub is local access function (A : Integer) return Integer;
> but this isn't sufficiently broken for such a change.]

If an object were passed instead of the pointer, there would be no this
problem at all. Pointers considered harmful.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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:31                   ` Randy Brukardt
  2011-01-01 16:46                 ` Yannick Duchêne (Hibou57)
  2 siblings, 2 replies; 36+ messages in thread
From: AdaMagica @ 2010-11-30  9:14 UTC (permalink / raw)
  Cc: randy

> No problem here, just use an expression function to give your expression a
> name (and parameters); then you can use 'Access just fine. Forget anonymous
> stuff, it only leads to heartache. :-)
>
>     function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
> else A);

Which AI is this? I can't find this syntax in RM 2012 Draft 10.



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
  1 sibling, 1 reply; 36+ messages in thread
From: Georg Bauhaus @ 2010-11-30 12:37 UTC (permalink / raw)


On 30.11.10 10:14, AdaMagica wrote:
>> No problem here, just use an expression function to give your expression a
>> name (and parameters); then you can use 'Access just fine. Forget anonymous
>> stuff, it only leads to heartache. :-)
>>
>>     function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
>> else A);
> 
> Which AI is this? I can't find this syntax in RM 2012 Draft 10.

http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai05s/ai05-0177-1.txt?rev=1.5
(found via the Schonberg presentation notes).

Question about the Ada-auth.org search programs:

search-acaa.cgi:

The results for "expression function" differ from those
for "expression functions" and AI 177 doesn't turn up
using the former parameter.


search-ai05s.cgi

Results differ, but AI 177 turns up (not a surprise, I should
think).

Q: Is it worth while adding singular or plural forms automatically?
Will doing so do any serious harm?  Copying the ubiquituous "+"
operator convention (indicating "as written, please!") can turn
this off, again.

(Word lists and algorithms are available in the free Unix
spelling libraries.)

http://www.ada-auth.org/search-acaa.cgi?SearchA=%22expression+functions%22&SearchO=&SearchN=

http://www.ada-auth.org/search-ai05s.cgi?SearchA=%22expression+functions%22&SearchO=&SearchN=



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada (User Data argument)
  2010-11-30  1:35       ` Randy Brukardt
@ 2010-11-30 16:51         ` Warren
  0 siblings, 0 replies; 36+ messages in thread
From: Warren @ 2010-11-30 16:51 UTC (permalink / raw)


Randy Brukardt expounded in news:id1kdp$27i$1@munin.nbi.dk:

> "Warren" <ve3wwg@gmail.com> wrote in message 
> news:Xns9E3F69D4E88CDWarrensBlatherings@188.40.43.230...
> ...
>> The problem with the "user data" argument, is that you
>> don't know what the user's type is going to be (in C, this
>> is given as a void *). The Ada dirty approach might be to
>> use 'System_Address, but that seems wrong. Still another
>> approach is to use a generic.
>>
>> Perhaps the best avenue might be to declare a base tagged
>> type, that the user can extend. Then the user data
>> argument could be the base classwide type.
> 
> That is the best avenue, unless the "user data" type can be
> (or needs to be) part of the "root" tagged object. For
> instance, in Claw, we used the "user data" type in order to
> provide a link between the Windows handle and the Claw
> object. That's OK in that if you need private "user data",
> you can just add it to your own extension of the Claw data
> type. 
> 
>                              Randy.

I see what you mean. Yep, I like that.

Warren



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-30 12:37                   ` Georg Bauhaus
@ 2010-11-30 20:28                     ` Randy Brukardt
  0 siblings, 0 replies; 36+ messages in thread
From: Randy Brukardt @ 2010-11-30 20:28 UTC (permalink / raw)


"Georg Bauhaus" <rm.dash-bauhaus@futureapps.de> wrote in message 
news:4cf4f014$0$6977$9b4e6d93@newsspool4.arcor-online.net...
> Question about the Ada-auth.org search programs:
>
> search-acaa.cgi:
>
> The results for "expression function" differ from those
> for "expression functions" and AI 177 doesn't turn up
> using the former parameter.

If you quote the search terms, it only looks for an exact match. (The search 
page notes this.) You can quote single words.

If you don't quote the search terms, it looks for forms of the word.

Quoted text of multiple words is far more expensive to search for (as the 
matching documents need to be checked to see if the words appear together) 
than unquoted text. Searching for forms would just be too much (it barely 
finishes in 15 seconds as it is on the wide search).

Also, when I search for "expression function" (in quotes), AI-177 is the 
third match. Not sure why you didn't see it (the index hasn't changed 
recently), so I should have gotten the same results that you did. Also note 
that the most recent versions of AI-177 probably aren't in the search index 
at all.

If I search for "expression function" without the quotes, AI-177 is also the 
third match.

                                     Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-30  9:14                 ` AdaMagica
  2010-11-30 12:37                   ` Georg Bauhaus
@ 2010-11-30 20:31                   ` Randy Brukardt
  1 sibling, 0 replies; 36+ messages in thread
From: Randy Brukardt @ 2010-11-30 20:31 UTC (permalink / raw)


"AdaMagica" <christoph.grein@eurocopter.com> wrote in message 
news:0deb178c-ecac-4560-8bae-a904432046ee@n2g2000pre.googlegroups.com...
>> No problem here, just use an expression function to give your expression 
>> a
>> name (and parameters); then you can use 'Access just fine. Forget 
>> anonymous
>> stuff, it only leads to heartache. :-)
>>
>> function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
>> else A);
>
>Which AI is this? I can't find this syntax in RM 2012 Draft 10.

It's mostly in section 6.8. It's a virtual section in Draft 10. :-) [AI-177 
wasn't approved until the most recent meeting, and I didn't complete the 
draft of the AI until just a few days before the meeting.] It'll be in Draft 
11, whenever that comes out.

                                    Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2010-11-30  1:32               ` Randy Brukardt
  2010-11-30  8:38                 ` Dmitry A. Kazakov
  2010-11-30  9:14                 ` AdaMagica
@ 2011-01-01 16:46                 ` Yannick Duchêne (Hibou57)
  2011-01-02 10:14                   ` AdaMagica
  2 siblings, 1 reply; 36+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-01-01 16:46 UTC (permalink / raw)


Le Tue, 30 Nov 2010 02:32:55 +0100, Randy Brukardt <randy@rrsoftware.com>  
a écrit:
> No problem here, just use an expression function to give your expression  
> a
> name (and parameters); then you can use 'Access just fine. Forget  
> anonymous
> stuff, it only leads to heartache. :-)
>
>     function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
> else A);
>
What compiler support this ?


-- 
Si les chats miaulent et font autant de vocalises bizarres, c’est pas pour  
les chiens.

“I am fluent in ASCII” [Warren 2010]



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-01 16:46                 ` Yannick Duchêne (Hibou57)
@ 2011-01-02 10:14                   ` AdaMagica
  2011-01-02 14:11                     ` Robert A Duff
  0 siblings, 1 reply; 36+ messages in thread
From: AdaMagica @ 2011-01-02 10:14 UTC (permalink / raw)


On 1 Jan., 17:46, Yannick Duchêne (Hibou57) <yannick_duch...@yahoo.fr>
wrote:
> >     function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
> > else A);
>
> What compiler support this ?

Of course no one - it's Ada 2012 or so (if and whenever it will be
appropeved)



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-02 10:14                   ` AdaMagica
@ 2011-01-02 14:11                     ` Robert A Duff
  2011-01-02 15:15                       ` AdaMagica
  2011-01-02 15:38                       ` Alex Mentis
  0 siblings, 2 replies; 36+ messages in thread
From: Robert A Duff @ 2011-01-02 14:11 UTC (permalink / raw)


AdaMagica <christoph.grein@eurocopter.com> writes:

> On 1 Jan., 17:46, Yannick Duch�ne (Hibou57) <yannick_duch...@yahoo.fr>
> wrote:
>> > � � function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
>> > else A);
>>
>> What compiler support this ?
>
> Of course no one - it's Ada 2012 or so (if and whenever it will be
> appropeved)

Actually, the latest version of GNAT supports the above,
and a bunch of other Ada 2012 features.

- Bob



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
  1 sibling, 1 reply; 36+ messages in thread
From: AdaMagica @ 2011-01-02 15:15 UTC (permalink / raw)


On 2 Jan., 15:11, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
> AdaMagica <christoph.gr...@eurocopter.com> writes:
> > On 1 Jan., 17:46, Yannick Duch ne (Hibou57) <yannick_duch...@yahoo.fr>
> > wrote:
> >> > function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
> >> > else A);
>
> >> What compiler support this ?
>
> > Of course no one - it's Ada 2012 or so (if and whenever it will be
> > appropeved)
>
> Actually, the latest version of GNAT supports the above,
> and a bunch of other Ada 2012 features.

The latest *supported* one that is, I guess.

Will GNAT GPL 2011 also support it?



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-02 15:15                       ` AdaMagica
@ 2011-01-02 15:28                         ` Robert A Duff
  0 siblings, 0 replies; 36+ messages in thread
From: Robert A Duff @ 2011-01-02 15:28 UTC (permalink / raw)


AdaMagica <christoph.grein@eurocopter.com> writes:

> On 2 Jan., 15:11, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
>> Actually, the latest version of GNAT supports the above,
>> and a bunch of other Ada 2012 features.
>
> The latest *supported* one that is, I guess.

The version I have is the latest sources at AdaCore,
which I built myself.  I work for AdaCore.
So yes, if you have AdaCore support, you can
use many Ada 2012 features today.

I'm not familiar with the timing/procedures for
when/how these sources get into the FSF version.

> Will GNAT GPL 2011 also support it?

I think so, but I'm not sure.

- Bob



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-02 14:11                     ` Robert A Duff
  2011-01-02 15:15                       ` AdaMagica
@ 2011-01-02 15:38                       ` Alex Mentis
  2011-01-02 15:44                         ` Robert A Duff
  1 sibling, 1 reply; 36+ messages in thread
From: Alex Mentis @ 2011-01-02 15:38 UTC (permalink / raw)


Robert A Duff wrote:

> function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
>                                                  else A);
 
...

> Actually, the latest version of GNAT supports the above,
> and a bunch of other Ada 2012 features.
> 
> - Bob

I was not able to get a short test program using the code above to
compile using GNAT GPL 2010 from AdaCore, even when using the Ada 2012
switches (-gnat12 and -gnat2012) and pragma (pragma Ada_2012).  The
error I get makes it appear that the parser is looking for a �begin�
for function Foobar.

Using the following worked, though:

function Foobar (A : Integer) return Integer is
begin
   return (if A > 10 then A - 5 else A);
end Foobar;

What am I missing?

-Alex



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-02 15:38                       ` Alex Mentis
@ 2011-01-02 15:44                         ` Robert A Duff
  2011-01-03 21:38                           ` Randy Brukardt
  0 siblings, 1 reply; 36+ messages in thread
From: Robert A Duff @ 2011-01-02 15:44 UTC (permalink / raw)


"Alex Mentis" <foo@invalid.invalid> writes:

> I was not able to get a short test program using the code above to
> compile using GNAT GPL 2010 from AdaCore, even when using the Ada 2012
> switches (-gnat12 and -gnat2012) and pragma (pragma Ada_2012).  The
> error I get makes it appear that the parser is looking for a �begin�
> for function Foobar.
>
> Using the following worked, though:
>
> function Foobar (A : Integer) return Integer is
> begin
>    return (if A > 10 then A - 5 else A);
> end Foobar;
>
> What am I missing?

I don't remember when these features were implemented.
Apparently, the GPL 2010 has 'if' expressions,
but expression functions are more recent.

- Bob



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
  0 siblings, 2 replies; 36+ messages in thread
From: Randy Brukardt @ 2011-01-03 21:38 UTC (permalink / raw)


"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message 
news:wcck4inqpij.fsf@shell01.TheWorld.com...
> "Alex Mentis" <foo@invalid.invalid> writes:
>
>> I was not able to get a short test program using the code above to
>> compile using GNAT GPL 2010 from AdaCore, even when using the Ada 2012
>> switches (-gnat12 and -gnat2012) and pragma (pragma Ada_2012).  The
>> error I get makes it appear that the parser is looking for a "begin"
>> for function Foobar.
>>
>> Using the following worked, though:
>>
>> function Foobar (A : Integer) return Integer is
>> begin
>>    return (if A > 10 then A - 5 else A);
>> end Foobar;
>>
>> What am I missing?
>
> I don't remember when these features were implemented.
> Apparently, the GPL 2010 has 'if' expressions,
> but expression functions are more recent.

That would make sense; I didn't actually write the wording for this proposal 
until mid-October (this was my AI). That was then approved at the late 
October ARG meeting. There are an outline of the feature before that, but no 
details; it would have been tough to implement it usefully without those 
details.

                                   Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-03 21:38                           ` Randy Brukardt
@ 2011-01-04  0:11                             ` Robert A Duff
  2011-01-04 18:33                             ` Alex Mentis
  1 sibling, 0 replies; 36+ messages in thread
From: Robert A Duff @ 2011-01-04  0:11 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message 
> news:wcck4inqpij.fsf@shell01.TheWorld.com...
>> I don't remember when these features were implemented.
>> Apparently, the GPL 2010 has 'if' expressions,
>> but expression functions are more recent.
>
> That would make sense; I didn't actually write the wording for this proposal 
> until mid-October (this was my AI). That was then approved at the late 
> October ARG meeting. There are an outline of the feature before that, but no 
> details; it would have been tough to implement it usefully without those 
> details.

Yeah, that makes sense.  AdaCore is trying to keep up with the Ada 2012
language, but of course there's danger: if ARG changes its collective
mind before the 2012 standard is standard, then AdaCore has to change
things, and people who choose to use those features will have to change
their code.  AdaCore's goal is to have Ada 2012 implemented properly
pretty soon after Ada 2012 is an official standard.

- Bob



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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:15                               ` Randy Brukardt
  1 sibling, 2 replies; 36+ messages in thread
From: Alex Mentis @ 2011-01-04 18:33 UTC (permalink / raw)


Randy Brukardt wrote:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
> news:wcck4inqpij.fsf@shell01.TheWorld.com...  >"Alex Mentis"
> <foo@invalid.invalid> writes:
> > 
> > > I was not able to get a short test program using the code above to
> > > compile using GNAT GPL 2010 from AdaCore, even when using the Ada
> > > 2012 switches (-gnat12 and -gnat2012) and pragma (pragma
> > > Ada_2012).  The error I get makes it appear that the parser is
> > > looking for a "begin" for function Foobar.
> > > 
> > > Using the following worked, though:
> > > 
> > > function Foobar (A : Integer) return Integer is
> > > begin
> >>   return (if A > 10 then A - 5 else A);
> > > end Foobar;
> > > 
> > > What am I missing?
> > 
> > I don't remember when these features were implemented.
> > Apparently, the GPL 2010 has 'if' expressions,
> > but expression functions are more recent.
> 
> That would make sense; I didn't actually write the wording for this
> proposal until mid-October (this was my AI). That was then approved
> at the late October ARG meeting. There are an outline of the feature
> before that, but no details; it would have been tough to implement it
> usefully without those details.
> 
>                                   Randy.

Since it�s the topic of discussion, and since you wrote the proposal,
is it true that expression functions (which, in effect, appear to be
function specifications that include small expressions as the function
bodies), when supported, could appear in a package specification,
rather than in the body, or did I miss-read the information I saw
online?

In other words, from what I read, it looks like once this is supported
one could have a function Foobar in an .ads file that looks like

function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
else A);

and not need a corresponding function in the .adb file.

-Alex



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  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
  1 sibling, 1 reply; 36+ messages in thread
From: Robert A Duff @ 2011-01-04 19:47 UTC (permalink / raw)


"Alex Mentis" <foo@invalid.invalid> writes:

> Since it�s the topic of discussion, and since you wrote the proposal,
> is it true that expression functions (which, in effect, appear to be
> function specifications that include small expressions as the function
> bodies), when supported, could appear in a package specification,

Yes.

One reason you might want to do that is when you call an
expression function in a precondition, and you want the compiler
or other tool to know what it does (at compile time of clients),
to help it prove things.

During the design, I argued that it's going to be annoying,
because as soon as you want a local variable inside that
function, you have to move it to the package body, weakening
the ability to prove things.  So we should simply allow
subprogram bodies in package specs.

Others argued that we don't want to give people the ability to
completely dispense with Ada's separation of spec and body.
Also, the expression function syntax is nice for short
"one liners", where the extra verbiage of "begin" and
"end Blah;" is just noise.

- Bob



^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-04 18:33                             ` Alex Mentis
  2011-01-04 19:47                               ` Robert A Duff
@ 2011-01-04 20:15                               ` Randy Brukardt
  1 sibling, 0 replies; 36+ messages in thread
From: Randy Brukardt @ 2011-01-04 20:15 UTC (permalink / raw)


"Alex Mentis" <foo@invalid.invalid> wrote in message 
news:ifvp6h$qga$1@news.eternal-september.org...
...
> In other words, from what I read, it looks like once this is supported
> one could have a function Foobar in an .ads file that looks like
>
> function Foobar (A : Integer) return Integer is (if A > 10 then A - 5
> else A);
>
> and not need a corresponding function in the .adb file.

Right.

An alternative way of looking at this feature is that it provides a way to 
give a name to an expression. (This is modeled as a function because it is 
the least disruptive way to do that; the original idea was to call this a 
"renaming" of an expression.) The "body" in this case is just the expression 
being named.

                         Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

* Re: Callback in Ada
  2011-01-04 19:47                               ` Robert A Duff
@ 2011-01-04 20:21                                 ` Randy Brukardt
  0 siblings, 0 replies; 36+ messages in thread
From: Randy Brukardt @ 2011-01-04 20:21 UTC (permalink / raw)


"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message 
news:wccmxngzc0x.fsf@shell01.TheWorld.com...
...
> Others argued that we don't want to give people the ability to
> completely dispense with Ada's separation of spec and body.
> Also, the expression function syntax is nice for short
> "one liners", where the extra verbiage of "begin" and
> "end Blah;" is just noise.

To expand on this a bit.

Ada always has had the ability to put arbitrary expressions into package 
specifications (default expressions, initializing expressions, etc.). This 
new capability just provides a way to give those expressions a convinient 
name.

OTOH, Ada only has one way to put a body into a package specification (via a 
generic instantiation - and it is rarely used for this purpose). Adding more 
ways to put a body into a package specification seems to change the model of 
the language, and might also pose new implementation difficulties 
(implementations might somehow depend on the absence of bodies). Indeed, it 
is likely that there are language rules which depend on this absence (I 
distinctly remember a freezing rule that only works because there cannot be 
a procedure call in a package specification, although I wasn't able to turn 
up a reference to that). So allowing full bodies in package specifications 
is a lot more risky than just allowing expressions.

                                Randy.





^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2011-01-04 20:21 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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