iterators again

Bill Baxter dnewsgroup at billbaxter.com
Sun May 27 17:46:03 PDT 2007


David B. Held wrote:
> Bill Baxter wrote:
>> What's the deal with making iterators be classes in D, rather than 
>> simple value types like they are in C++?  I see that tango uses 
>> classes, and DTL used them too.  Chris commented over on the Tango 
>> forum that "there are problems with using structs".  So what are they?
>>
>> The way people describe iterators in C++ is as a generalization of a 
>> pointer.  Including the fact that they are value types and can be 
>> copied.  DTL and tango iterators can't even be copied at all.  Just 
>> seems like a struct makes more sense for an iterator in D.
> 
> Iterators should almost certainly be value types (structs).  The 
> problems are that structs currently don't have c'tors/d'tors nor do they 
> have a fully useful copy c'tor.  However, both are planned for inclusion.

Hmm, I can't think of a case where any of that would matter.
Looking at a few DTL iterators, none of them seem to contain anything 
that couldn't be done with static opCall.  I do see a somewhat dubious 
constructor in stack.d, however:

         this(value_type[] elements, index_type position)
         {
             m_position  =   position;
             m_elements  =   elements[position .. elements.length];
         }

This sets the size_t m_position to 10, say, but that becomes position 
zero in the slice m_elements.  Looks like a bug to me.

And classes don't have fully useful copy c'tors either, so you can't 
really hold that against structs.

The only think I've thought of (and this is probably Tango's main 
justification for classes) is that structs can't inherit.

--bb



More information about the Digitalmars-d mailing list