Are iterators and ranges going to co-exist?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jul 21 10:33:26 PDT 2010


Peter Alexander wrote:
> == Quote from Walter Bright (newshound2 at digitalmars.com)'s article
>> If some algorithms used ranges and others used iterators, the
> poor programmer
>> would find himself then obliged to implement both interfaces for
> each container.
> 
> This makes no sense.
> 
> I don't understand why you see ranges and iterators as conflicting
> concepts. The cursors/iterators are for referring to individual
> elements, and ranges are for specifying iterations over a sequence.
> 
> There aren't two competing interfaces here. If your interface
> requires a range of elements, you use a range. If it only needs a
> single element, you use a cursor.
> 
> For example, sort would always take a range, as it is sorting a
> range, not a single element. I *do not* propose that we start
> writing sort(Iter begin, Iter end) as that is much less flexible
> than a range, and as you say, it is much harder to validate.
> 
> An example of an algorithm that would take a cursor is
> insertBefore. There is no reason for it to accept a range, because
> you are not traversing elements. You just want to insert something
> before some single point in the range.

This contradicts the assertion that there's no competition between 
iterators and ranges. Clearly they share at least some turf. Though I 
agree "insert before this given element" is sensible, I don't think 
"insert before this range" is not. Besides, with ranges the notion is 
easier to generalize to "insert after this range" and "replace this 
range with another range". Arguably "insert after this one element" and 
"replace this one element with a range" are less general.

There are benefits to having cursors. There are also disadvantages: 
interfaces and implementations get bloated, users get aggravated, 
documentation gets thicker, conceptual overhead rises, design options 
multiply without necessity ("should I provide replace with one iterator 
in addition to replace with a range?"), writing code becomes more 
painful ("heck, I need a range here but I only have an iterator (or vice 
versa), I need to insert this extra construct here to build the other") 
etc. etc. I understand you want to advocate iterators so you chose to 
not discuss the disaadvantages, but any plan that would ignore them 
would not be complete.


Andrei


More information about the Digitalmars-d mailing list