comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin@thedowies.com>
Subject: Re: Ada202X : Adding functors
Date: Tue, 13 Nov 2012 03:11:57 -0800 (PST)
Date: 2012-11-13T03:11:57-08:00	[thread overview]
Message-ID: <57bca956-2348-4825-8f5f-04fb91863696@googlegroups.com> (raw)
In-Reply-To: <2bb9e5fa-04a2-4073-bca1-1739ce0580f1@googlegroups.com>

On Monday, November 12, 2012 11:44:39 PM UTC, Adam Beneschan wrote:
> On Monday, November 12, 2012 2:09:15 PM UTC-8, Martin wrote:
> 
> > I'm increasingly using functors in my C++ life and they are very cool. I'd like to be able to use them in my Ada life too but the language doesn't support it - or can someone tell me I'm wrong?
> 
> > 
> 
> > I think it could be done by 'simply' allowing something like this:
> 
> > 
> 
> > package P is 
> 
> >    type T is tagged ...;
> 
> > 
> 
> >    function Create return T;
> 
> >    procedure "()" (This : T; I : Integer);  -- The functor call
> 
> >    function "()" (This : T) return Integer; -- Alternative functor that's a function
> 
> > 
> 
> > private
> 
> > 
> 
> > ...
> 
> > 
> 
> > end P;
> 
> > 
> 
> > with P;
> 
> > procedure Demo is
> 
> >    My_T : P.T := P.Create
> 
> > begin
> 
> >    My_T (42);  -- call to procedure version
> 
> >    if My_T () > 100 then ... end if;  -- call to function version
> 
> > end Demo;
> 
> > 
> 
> > 
> 
> > 
> 
> > I guess one of the trickier bits of adding this would be the possible need to step away from the current Ada convention of not requiring empty parenthesis...but new language revisions always change the 'current' good style...
> 
> > 
> 
> > Any thoughts?
> 
> 
> 
> package P is 
> 
>    type T is tagged ...; 
> 
>    function Create return T; 
> 
>    procedure Call (This : T; I : Integer);  -- The functor call 
> 
>    function Call (This : T) return Integer; -- Alternative functor that's   
> 
>                                             -- function
> 
> private 
> 
> ... 
> 
> end P; 
> 
> 
> 
> with P; 
> 
> procedure Demo is 
> 
>    My_T : P.T := P.Create ;
> 
> begin 
> 
>    My_T.Call (42);  -- call to procedure version 
> 
>    if My_T.Call > 100 then ... end if;  -- call to function version 
> 
> end Demo; 
> 
> 
> 
> 
> 
> This already works in Ada, and doesn't require any major language changes that would give the ARG headaches about empty parentheses, possible syntax conflicts with user-defined references and indexing, the interpretation of constructs involving function calls that return P.T followed by parentheses, etc., etc., etc.  I'll concede that doing it this way may be considered less "cool" than being able to avoid typing that extra identifier ("Call").  Being able to say "This is a variable, but OH LOOK I can use it like a function" certainly does seem cool, in a way.  I don't see any other advantage, though.
> 
> 
> 
>                          -- Adam

But Adam - that's cool cat is already out the bag! E.g.

with Ada.Text_IO; use Ada.Text_IO;
with Project;     use Project;
with Matrix_3x3s; use Matrix_3x3s;
with Vector_3s;   use Vector_3s;
procedure Test is
   procedure Display (X : Real) is
   begin
      Put_Line (Real'Image (X));
   end Display;
   V : Vector_3 := Create (X => 12.34,
                           Y => 123.4,
                           Z => 1234.0);
   M : Matrix_3x3 := (Create (X => V,
                              Y => V * 2.0,
                              Z => V * 4.0));
begin
   V (1) := 1.0;
   Display (V (1));
   Display (V (2));
   Display (V (3));
   M (1, 1) := 20.0;
   Display (M (1, 1));
end Test;

V and M used as-if they were functions - in fact they use Constant_Indexing and Variable_Indexing:

...
package Vector_3s is
   pragma Pure (Vector_3s);
   subtype An_Axis is Integer range 1 .. 3;
   type Vector_3 is tagged private
     with Constant_Indexing => Vector_3s.Constant_Reference,
          Variable_Indexing => Vector_3s.Variable_Reference;
...
package Matrix_3x3s is
   pragma Pure (Matrix_3x3s);
   subtype An_Axis is Integer range 1 .. 3;
   type Matrix_3x3 is tagged private
     with Constant_Indexing => Matrix_3x3s.Constant_Reference,
          Variable_Indexing => Matrix_3x3s.Variable_Reference;
...

But it doesn't currently work without an index parameter!

-- Martin



  parent reply	other threads:[~2012-11-13 11:11 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-12 22:09 Ada202X : Adding functors Martin
2012-11-12 23:14 ` Jeffrey Carter
2012-11-12 23:19   ` Martin
2012-11-13 10:45     ` Georg Bauhaus
2012-11-13 12:08       ` Yannick Duchêne (Hibou57)
2012-11-13 12:35       ` Martin
2012-11-12 23:44 ` Adam Beneschan
2012-11-13  2:11   ` Jeffrey Carter
2012-11-13 11:11   ` Martin [this message]
2012-11-14  0:51     ` Adam Beneschan
2012-11-14  8:39       ` Martin
2012-11-14 16:54         ` Adam Beneschan
2012-11-14 18:54           ` Martin
2012-11-14 20:37           ` Dmitry A. Kazakov
2012-11-14 20:57             ` Shark8
2012-11-14 21:31             ` Martin
2012-11-14 22:42               ` Adam Beneschan
2012-11-15  9:27               ` Dmitry A. Kazakov
2012-11-14 21:45             ` Simon Wright
2012-11-14 22:22               ` Martin
2012-11-14 22:27               ` Martin
2012-11-13  4:22 ` Shark8
2012-11-15  0:20 ` sbelmont700
2012-11-15  7:12   ` Martin
2012-11-15 12:21     ` Georg Bauhaus
2012-11-15 12:31     ` Georg Bauhaus
2012-11-15 12:46       ` Martin
2012-11-16  6:15       ` Randy Brukardt
2012-11-15 10:11   ` Dmitry A. Kazakov
2012-11-15 15:52     ` Peter C. Chapin
2012-11-15 17:04       ` Dmitry A. Kazakov
2012-11-15 19:57         ` Georg Bauhaus
2012-11-15 20:39           ` Dmitry A. Kazakov
2012-11-16  0:15             ` Peter C. Chapin
2012-11-16 10:12               ` Georg Bauhaus
2012-11-16  0:01         ` Peter C. Chapin
2012-11-16  6:09           ` Randy Brukardt
2012-11-16 12:35             ` Peter C. Chapin
2012-11-16 18:10               ` Martin
2012-11-16  8:59           ` Dmitry A. Kazakov
2012-11-16 12:20             ` Peter C. Chapin
2012-11-16 17:44               ` Dmitry A. Kazakov
2012-11-18 15:58                 ` Peter C. Chapin
2012-11-15 21:34     ` sbelmont700
replies disabled

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