comp.lang.ada
 help / color / mirror / Atom feed
From: bj.mooremr@gmail.com
Subject: Re: Two approaches of iterators for the key-value pairs
Date: Fri, 27 Nov 2015 14:52:05 -0800 (PST)
Date: 2015-11-27T14:52:05-08:00	[thread overview]
Message-ID: <dd8a9040-3ca6-486e-a4c3-2a316b8ae4dd@googlegroups.com> (raw)
In-Reply-To: <3a6f986a-f5f3-4175-b191-b0133e70ff66@googlegroups.com>

On Friday, November 27, 2015 at 11:08:25 AM UTC-7, ytomino wrote:
> On Saturday, November 28, 2015 at 1:30:23 AM UTC+9, Dmitry A. Kazakov wrote:
> > 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.
> 
> I wish dot-notation for non-tagged type is allowed (with pragma?)

My initial implementation of this was as an Iterator, rather than an Iterable container, and in my implementation, I got things to work this way without the Cursor being a tagged type.

My "proof of concept" implementation was for Ada.Directories, but the same approach would also work for Ada.Environment_Variables.

For Ada.Directories, I declared the Cursor type as;

type Cursor
  (Directory_Entry : not null access constant Directory_Entry_Type) is private
   with  Implicit_Dereference => Directory_Entry;

The Implicit_Dereference aspect was added to Ada 2012.

For Ada.Environment_Variables, I would have declared something like;

type Cursor
  (Name_Value_Pair : not null access constant Name_Value_Pair_Type) is private
   with  Implicit_Dereference => Name_Value_Pair;

And

   function Has_Element (Name_Value_Pair : Cursor) return Boolean;

   package Environment_Variable_Iterator_Interfaces is new
    Ada.Iterator_Interfaces
     (Cursor      => Cursor,
      Has_Element => Has_Element);

   function All_Variables
     return Environment_Variable_Iterator_Interfaces.Forward_Iterator'Class;


Then one would be able to write;

for Variable in All_Variables loop 
   Put_Line (Variable.Name & "=" & Variable.Value); 
end loop; 

Exactly as Dmitry had requested...

Brad


  parent reply	other threads:[~2015-11-27 22:52 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
2015-11-27 22:52     ` bj.mooremr [this message]
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