Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Won't Fix
-
1.0-beta-8
-
None
-
None
-
N/A
Description
Having both .. and ... as range operators can be confusing, because they are visually so similar. Also, the "cleanest" implementation of an idea can be confusing. For example, if you have a list of N elements, looping over all indicies can be done as:
for (i in 0...N) {
// blah
}
However, in my project, I need to compute something for all (integral) times from 0 to T, that is, T+1 values. Given the two range operators, the cleanest code is to use .. like this:
for (t in 0..T) {
// blah
}
However, someone reading that code could be forgiven for missing the subtle difference, and thinking it's the same form as the code snippet. It would be clearer to write:
for (t in 0...T+1) {
// blah
}
The problem is that the visual difference is so small; it's literally the smallest visual difference possible when deleting a single character. I suggest that, in the interests of keeping the base language as simple and elegant as possible, two range operators aren't needed, and in fact cause more confusion than whatever benefits they bring.
Given that Groovy's arrays & lists are zero based, I suggest keeping the form "include the lower bound but not the upper." FWIW, that's the same as Python.
At the very least, it's confusing that three dots is the smaller range than two dots. It's more intuitive to me that a...b would be the bigger range (greater distance between beginning and end) than a..b.