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

Andrey Tarantsov andrey at tarantsov.com
Wed Mar 23 15:48:41 CDT 2016


PS:

> endIndex is exclusive by definition

If you want a rationale on this...

1) It's how cursors typically work, and people familiar with the concept already expect this.

2) It's often the most convenient choice. Back in the day, you would write C code like this:

	for (int *cur = array, *end = array + size; cur < end; ++cur) {
            process(*cur);
        }

    Similarly, you can compute the number of elements by just getting the distance between startIndex and endIndex (or end - cur in the C example above).

    Making the upper boundary inclusive would require explicit '- 1' and '+ 1' in quite a few places, which is ugly and unnecessary.

3) The upper bound is almost always excluded in programming, in many unrelated APIs (e.g. arc4random_uniform), so it's a good default choice.

A.



More information about the swift-evolution mailing list