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: a07f3367d7,2078ce7aac45af5b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.224.219.144 with SMTP id hu16mr8536069qab.1.1352805117509; Tue, 13 Nov 2012 03:11:57 -0800 (PST) Received: by 10.52.34.205 with SMTP id b13mr4343587vdj.3.1352805117484; Tue, 13 Nov 2012 03:11:57 -0800 (PST) Path: gf5ni18188417qab.0!nntp.google.com!c7no24053604qap.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 13 Nov 2012 03:11:57 -0800 (PST) In-Reply-To: <2bb9e5fa-04a2-4073-bca1-1739ce0580f1@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=20.133.0.8; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G NNTP-Posting-Host: 20.133.0.8 References: <0114d327-9f9f-4ad2-9281-56331d11a90c@googlegroups.com> <2bb9e5fa-04a2-4073-bca1-1739ce0580f1@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <57bca956-2348-4825-8f5f-04fb91863696@googlegroups.com> Subject: Re: Ada202X : Adding functors From: Martin Injection-Date: Tue, 13 Nov 2012 11:11:57 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2012-11-13T03:11:57-08:00 List-Id: 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: >=20 > > 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? >=20 > >=20 >=20 > > I think it could be done by 'simply' allowing something like this: >=20 > >=20 >=20 > > package P is=20 >=20 > > type T is tagged ...; >=20 > >=20 >=20 > > function Create return T; >=20 > > procedure "()" (This : T; I : Integer); -- The functor call >=20 > > function "()" (This : T) return Integer; -- Alternative functor that= 's a function >=20 > >=20 >=20 > > private >=20 > >=20 >=20 > > ... >=20 > >=20 >=20 > > end P; >=20 > >=20 >=20 > > with P; >=20 > > procedure Demo is >=20 > > My_T : P.T :=3D P.Create >=20 > > begin >=20 > > My_T (42); -- call to procedure version >=20 > > if My_T () > 100 then ... end if; -- call to function version >=20 > > end Demo; >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > I guess one of the trickier bits of adding this would be the possible n= eed to step away from the current Ada convention of not requiring empty par= enthesis...but new language revisions always change the 'current' good styl= e... >=20 > >=20 >=20 > > Any thoughts? >=20 >=20 >=20 > package P is=20 >=20 > type T is tagged ...;=20 >=20 > function Create return T;=20 >=20 > procedure Call (This : T; I : Integer); -- The functor call=20 >=20 > function Call (This : T) return Integer; -- Alternative functor that's= =20 >=20 > -- function >=20 > private=20 >=20 > ...=20 >=20 > end P;=20 >=20 >=20 >=20 > with P;=20 >=20 > procedure Demo is=20 >=20 > My_T : P.T :=3D P.Create ; >=20 > begin=20 >=20 > My_T.Call (42); -- call to procedure version=20 >=20 > if My_T.Call > 100 then ... end if; -- call to function version=20 >=20 > end Demo;=20 >=20 >=20 >=20 >=20 >=20 > 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 parenthese= s, 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 fu= nction" certainly does seem cool, in a way. I don't see any other advantag= e, though. >=20 >=20 >=20 > -- 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 :=3D Create (X =3D> 12.34, Y =3D> 123.4, Z =3D> 1234.0); M : Matrix_3x3 :=3D (Create (X =3D> V, Y =3D> V * 2.0, Z =3D> V * 4.0)); begin V (1) :=3D 1.0; Display (V (1)); Display (V (2)); Display (V (3)); M (1, 1) :=3D 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 =3D> Vector_3s.Constant_Reference, Variable_Indexing =3D> 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 =3D> Matrix_3x3s.Constant_Reference, Variable_Indexing =3D> Matrix_3x3s.Variable_Reference; ... But it doesn't currently work without an index parameter! -- Martin