comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Two approaches of iterators for the key-value pairs
Date: Fri, 27 Nov 2015 21:50:43 +0100
Date: 2015-11-27T21:50:43+01:00	[thread overview]
Message-ID: <149pnl1e6viam.1hdd2zp8f7y3r.dlg@40tude.net> (raw)
In-Reply-To: 3a6f986a-f5f3-4175-b191-b0133e70ff66@googlegroups.com

On Fri, 27 Nov 2015 10:08:23 -0800 (PST), ytomino wrote:

> On Saturday, November 28, 2015 at 1:30:23 AM UTC+9, Dmitry A. Kazakov wrote:
>> On Fri, 27 Nov 2015 07:25:57 -0800 (PST), ytomino wrote:
>> 
>>> Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
>>> http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4
>>> 
>>> And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
>>> (The boolean trick of these new iterators is also interesting, but set aside.)
>>> 
>>> A loop for Ada.Environment_Variables would be like below, according to this AI:
>>> 
>>> for E *of* Ada.Environment_Variables.All_Variables loop
>>>    -- E is Iterator_Element (Name_Value_Pair_Type)
>>>    Name (E) -- key
>>>    Value (E) -- value
>>> end loop;
>>> 
>>> On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
>>> 
>>> for I *in* The_Map_Object.Iterate loop
>>>    -- I is Cursor
>>>    Key (E) -- key
>>>    Element (E), Reference (E).Element.all -- value
>>> end loop;
>>> 
>>> If you create new iterator for some key-value pairs, which approach do you like?
>> 
>> Rather this:
>> 
>> for Variable in All_Variables loop
>>    Put_Line (Variable.Name & "=" & Variable.Value);
>> end loop;
> 
> It sounds good.
> 
> However, the Cursor probably have to be tagged to implement it for
> dot-notation, and, I think it will be an double-dispatching error on
> First/Next because the iterator is also tagged.

Why should it be cursor? As Pascal said, Variable is a plain record type,
the element of the container (seen as a set).

And of course indices must work too:

   for Key in All_Variables.Keys loop
      Put_Line (Key & "=" & All_Variables (Key));
   end loop;

'Range instead of .Keys would be even better, but it is probably too much
to ask.

> I wish dot-notation for non-tagged type is allowed (with pragma?)

There is already mechanism for this:

   function Add (X, Y : T) return T;
   function "+" (X, Y : T) return T renames Add;

This is not any different to

   function Name (X : T) return String;
   function ".Name" (X : T) return String renames Name;

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

  reply	other threads:[~2015-11-27 20:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-27 15:25 Two approaches of iterators for the key-value pairs ytomino
2015-11-27 16:30 ` Dmitry A. Kazakov
2015-11-27 18:08   ` ytomino
2015-11-27 20:50     ` Dmitry A. Kazakov [this message]
2015-11-27 22:52     ` bj.mooremr
2015-11-27 17:00 ` Pascal Obry
2015-11-27 18:25   ` ytomino
2015-11-27 17:43 ` Brad Moore
2015-11-27 19:38   ` ytomino
2015-11-27 19:46     ` ytomino
2015-11-27 23:11     ` Brad Moore
2015-11-28  8:58       ` Simon Wright
2015-11-28 19:54         ` Brad Moore
2015-11-28 23:34           ` Simon Wright
2015-11-29 21:17             ` Bob Duff
2015-11-29 16:17         ` Simon Wright
2015-11-29 17:55       ` ytomino
replies disabled

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