Range's opSlice(/**/) function

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 10 14:51:32 PDT 2012


On Tuesday, July 10, 2012 23:35:02 monarch_dodra wrote:
> I've noticed that some ranges in Phobos don't implement
> "opSlice(/**/)", when they provide "opSlice(indexLow, indexHigh)".
> 
> For example, algorithm.map, or Array.Range don't provide it.
> 
> I was wondering if this was just an oversight of the
> implementation, or if there was a special reason to choose not to
> provide it.
> 
> I've taken to writing: "writeln(stuff[])" whenever I want to
> print a collection of things, to make sure I don't just print the
> name of the "stuff" class. I don't stop and wonder if stuff is a
> range or not, and just append it []. I was surprised it did not
> work consistently.
> 
> ...Or is there a reason I should stop writing "stuff[]"?

1. In general, I wouldn't recommend not caring whether a variable is a 
container or a range. It matters a great deal to the semantics of what you're 
doing.

2. At this point, [] is primarily used for getting ranges from containers, for 
slicing static arrays, and for array copy operations.

a[] = b[];

You can't depend on any range defining it. None of the isXRange templates 
require it.

That being said, there's no reason why at least some ranges couldn't define it. 
However, it would probably have to be restricted to forward ranges (making it 
the same as save), so you still wouldn't be able to just assume that [] would 
work. And unless we add it to what isForwardRange requires, you couldn't even 
rely on forward ranges having it, since it's not required of them.

- Jonathan M Davis


More information about the Digitalmars-d mailing list