Issue with forward ranges which are reference types

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 16 21:28:35 PDT 2011


On Tuesday, August 16, 2011 21:17:31 Mehrdad wrote:
> On 8/16/2011 9:05 PM, Jonathan M Davis wrote:
> > Sorry that this is long, but it's very important IMHO, and I don't know
> > how to make it much shorter and cover what it's supposed to cover.
> > 
> > Okay. Your typical forward range is either an array a struct which is a
> > value type (that is, copying it creates an independent range which
> > points to the same elements and is not altered if the original range is
> > altered - the elements that it points to aren't copied of
> > course).<snip>
> > Thoughts?
> > 
> > - Jonathan M Davis
> 
> Funny, I was also thinking about this recently.
> 
> The trouble is that that's not the only issue. There's also the issue
> with polymorphism -- i.e., InputRangeObject is pretty much *useless*
> right now because no function ever checks for it (AFAIK... am I wrong?).
> So if you pass a random-access range object as an InputRange, the callee
> will just assume it's an InputRange and would reject it. So you're
> forced to downcast every time, which is really tedious. Things don't
> "just work" anymore.

Phobos' functions pretty much always have template constraints to verify that 
they're given the correct type of range, and if they don't then they're 
supposed to. So, lots of functions have isInputRange!R, and most of the other 
range types are subtypes of input ranges, so checking for them also checks for 
input ranges - e.g. isForwardRange!R.

Polymorphism has _nothing_ to do with the range API though. If you're dealing 
with a range type which is a class or interface, then it must implement all of 
the appropriate functions for the type (or types) of range(s) that it's 
supposed to be. If the calls happen to be polymorphic, that's fine, but the 
range-based functions don't care. Whether a type is a particular type of range 
or not is _entirely_ a matter of its API.

So, I don't quite understand what your issue is here.

- Jonathan M Davis


More information about the Digitalmars-d mailing list