www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Range's opSlice(/**/) function

reply "monarch_dodra" <monarch_dodra gmail.com> writes:
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[]"?
Jul 10 2012
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Jul 10 2012
prev sibling parent "monarch_dodra" <monarch_dodra gmail.com> writes:
On Tuesday, 10 July 2012 at 21:51:44 UTC, Jonathan M Davis wrote:
 On Tuesday, July 10, 2012 23:35:02 monarch_dodra wrote:

 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

Thank you for your reply, it makes a lot of sense.
Jul 11 2012