Mega Search
23.2 Million


Sign Up

Make a donation  
Some problems with iterators  
News Group: comp.lang.ada

Why Ada2012 does allow only type name for an iterator in for loops, not an 
iterator object?

I think, it is a design mistake. We should also accept iterator object, 
probably constructed earlier.

Now, it is probably possible to do this by introducing a dummy container 
(used only to initialize my iterator), but this is a hack. We should allow 
creation of iterators without unnecessary containers.

The following does not verify with GCC 4.9.1 (I think it is a compiler bug 
and have reported this bug in GCC Bugzilla):

with Ada.Iterator_Interfaces;

package Test is
   
   type Description_Cursor is new Natural;

   function Has_Element (Position: Description_Cursor) return Boolean;

   package Description_Iterators  is new 
Ada.Iterator_Interfaces(Description_Cursor, Has_Element);

   type Description_Iterator is new Description_Iterators.Forward_Iterator 
with null record;

   overriding function First (Object: Description_Iterator) return 
Description_Cursor;
   overriding function Next (Object: Description_Iterator; Position: 
Description_Cursor) return Description_Cursor;

   type Descriptions_List is tagged limited null record
     with Default_Iterator=>Get_Descriptions_Iterator;

   not overriding function Get_Descriptions_Iterator (List: 
Descriptions_List'Class) return Description_Iterator;

end Test;

However, if I remove 'Class in the declaration of Get_Descriptions_Iterator 
function, it does not work either:

test.ads:19:28: operation can be dispatching in only one type

This may indicate one more design error: I cannot define Default_Iterator of 
a suitable subprogram signature. Or is there any way to work around this 
problem? How to define it? Please show example code.

-- 
Victor Porton - http://portonvictor.org

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 20-Aug-2014, at 7:48 PM EST
From: Victor Porton