Iterators for D

Bill Baxter dnewsgroup at billbaxter.com
Mon Nov 6 15:58:18 PST 2006


Hasan Aljudy wrote:
> 
> 
> rm wrote:
>> Walter Bright wrote:
>>> Walter Bright wrote:
>>>> .being property returns an iterator that starts at the beginning
>>>
>>> Make that .begin
>>>
>>> Also, overloading += will be used to advance the iterator.
>>
>> that would give something like this?
>>
>> import std.stdio;
>>
>> void main()
>> {
>>     char[] s = "hello";
>>     // += for advancing an iterator, why not ++?
>>     for (iterator i = s.begin; i != s.end; i += 1)
>>         writefln(s[i]);
>>
>>     // ??? i is an iterator or a char  ???
>>     foreach(i; s)
>>         // extend writefln to take iterators ?
>>         // but that would mean that it must be possible
>>         // to determine a value from an iterator
>>         writefln(i);
>> }
>>
> 
> Why would I use
>  >     for (iterator i = s.begin; i != s.end; i += 1)
>  >         writefln(s[i]);
> 
> instead of:
> 
>  >     for (int i = 0; i < s.length; i++)
>  >         writefln(s[i]);
> 
> ?

The point is that you could replace char[] with SomeCollection and the 
code should still work.  But the same mechanism should also work on the 
built-in types.

--bb



More information about the Digitalmars-d mailing list