[swift-evolution] [Pitch] Change the endIndex value for closed Ranges and Collections

Dave Abrahams dabrahams at apple.com
Wed Mar 23 19:15:18 CDT 2016

on Wed Mar 23 2016, Pedro Vieira <swift-evolution at swift.org> wrote:

> Hi Swift developers,
> I hereby present my first Swift proposal regarding the endIndex on
> `Collections` and closed `Ranges`. I’ve searched the mailing list archives
> for something similar to this but couldn’t find it, so I decided to come
> forward.
> *The problem:*
> I was recently working on a library and used a closed Range to define the
> bounds of a board game and the tests that used its endIndex were all
> failing. Started debugging it around and, to my complete surprise, found
> out that the endIndex of the closed Ranges was always +1 from the value I
> initially set. With this, I decided to dive into the Range source code and
> discovered that all closed ranges are converted to half-open ones when
> initialized:
> a) 1..<10 stays 1..<10 (endIndex = 10)
> b) 1...10 is converted to 1..<11 (endIndex = 11)
> To work around this behavior I had to subtract 1 every time I checked for
> the endIndex, since I didn't want to use last! (force unwrapping) or
> if-let, which ultimately polluted my code. 

Please see the work currently underway in
which we intend to propose soon.  ClosedRange becomes a separate type
that represents its upperBound without modification.


More information about the swift-evolution mailing list