foreach vs. iterators

Sean Kelly sean at f4.ca
Sun Nov 5 11:06:07 PST 2006


Bill Baxter wrote:
> Sean Kelly wrote:
>> Sean Kelly wrote:
>>
>>> Still, I think it's useful to have in an iterator, so perhaps 
>>> something roughly like this:
>>>
>>> interface Iterator(T)
>>> {
>>>     T val();
>>>     T next();
>>>     bool hasNext();
>>> }
>>
>>
>> Err... this doesn't work for an iterator returned for an empty 
>> container.  
> 
> It's fine if you define the behavior to be that you have to call next() 
> once to get the first value.  So for iteration idiom becomes:
> 
> T v;
> while(c.hasNext()) )
> {
> }

Yup, but this seems confusing with the presence of val() methods.



More information about the Digitalmars-d-announce mailing list