Swift language should stop using 0-based array indexing

Originator:mauricerkelly
Number:rdar://17628428 Date Originated:10-Jul-2014 08:59 PM
Status:Closed Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
 
Update from Apple:

Apple Developer Relations11-Jul-2014 04:51 PM

Engineering has determined that this issue behaves as intended based on the following:

Swift is intended to feel like a member of the extended family of C derived languages, changing this would break that.

We are now closing this bug report.

=========

Original bug report:

Summary:
Since time immemorial we have been using 0-based array indexing with our programming languages. While the origin of this practice isn't always clear, many accept that it is either a mathematical practice, or (more frequently) because of pointer offsets.

We have been repeatedly told that Swift is a modern programming language, yet it maintains this ancient practice of 0-based indexing. Apple has even created a new "half-closed range operator" whose primary use-case seems to be 0..count. For a language with so many features that seem perfect for newcomers to programming, maintaining 0-based indexing seems like a needless complexity. We no longer have to worry about pointer arithmetic, and very few developers are mathematicians. 

I'd like to request that the Swift development team take this opportunity to break with the past and do away with 0-based indexing.

Steps to Reproduce:
1. Create an array using let myArray = [ 1, 2, 3 ]
2. Attempt to print the value of the second element of the array

Expected Results:
3. I should use the syntax println("Second element is \(myArray[2])")

Actual Results:
3. I have to use the syntax println("Second element is \(myArray[1])")

Comments

Personally I think it would have been possible - Apple are already doing bridging between Foundation/Swift types, and as an educated guess I'd say they could probably handle the "conversion". Not gonna happen though - got an update from them :-)

By mauricerkelly at July 12, 2014, 11:15 a.m. (reply...)

Could this be compatible with Objective-C?

Regardless of the merits of 1-based indexing, doing this with Swift would make it difficult to mix-and-match with Objective-C code, which presumably would continue to use 0-based indexes. Whenever you passed indexes between Swift and Objective-C code, you would need to remember to add or subtract one (or let the compiler try to magically figure it out for you, which might be even worse).

Microsoft made this mistake with COM: They allowed component implementers to decide whether to use 0-based indexes or 1-based indexes. Components implemented in C++ usually used 0-based indexes, while components implemented in Visual Basic used 1-based indexes, and keeping it all straight was a nightmare.

By kristopherdjohnson at July 11, 2014, 6:32 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!