So how exactly does one make a persistent range object?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 5 18:02:00 PDT 2011


On 2011-06-05 17:47, Andrej Mitrovic wrote:
> Yeah I'm referring to polymorphism.
> 
> If I want to provide some sort of interface for my users that let them
> build any type of range at runtime, or even a range of ranges, then
> there's polymorphism involved. Other than polymorphism there's no
> other way a function can take or return a generic range type at
> runtime without know its exact type at compile-time.
> 
> Of course a polymorphic range of ranges is going to be a performance
> bottleneck due to virtual function calls, so it doesn't make sense for
> ranges to be polymorphic in the first place.
> 
> I don't have a real use-case for these kinds of runtime-manipulated
> range types yet. I'd have to build something before I could make some
> kind of a case for them. So I'm really just speculating on what I
> need, and I'm experimenting with what ranges have to offer. I'll see
> how far I can go with the typeof() trick. :)

Well, iterators aren't generally polymorphic beyond perhaps dealing with the 
fact that the container that they iterate over is polymorphic (which happens 
in Java and C#, but not standard C++). However, ranges can obviously be used 
for rather more than iterators, so that complicates things.

Ranges _can_ be polymorphic (forward ranges' save function makes it possible 
to deal with ranges which are class types), but Phobos' ranges aren't 
polymorphic. So, anything you do on your own could be polymorphic, but as soon 
as you get ranges from Phobos, you lose the polymorphism. But in the vast 
majority of cases, I don't expect that that will be an issue at all. What 
you'll likely be forced to do in some cases is copy a range's values into a 
new container, but that's exactly what you generally have to do in C++, Java, 
and C# anyway. So, it's not really much of a loss.

On the whole, I believe that ranges were generally intended to be processed 
and then tossed, which is usually what happens with iterators, so the sorts of 
issues that you're talking about are a bit out of the norm. People do keep 
iterators and ranges around some of the time, but generally, if they want to 
keep that data long term, they put it in a new container - particularly when 
you consider the fact that so many iterators and ranges can change out from 
under you.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list