CollectionType subscript should return an optional
| Originator: | davbeck | ||
| Number: | rdar://19996812 | Date Originated: | 28-Feb-2015 10:15 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 1.2 |
| Classification: | Other Bug | Reproducible: | Always |
When accessing an element at a certain index, that index could be out of bounds. Everywhere else in swift where this situation would occur, an optional is returned to make it clear that a value may not be present and to avoid unnecessary crashes.
This also would avoid clunky boilerplate. Consider the following:
var element: AnyObject?
if array.count >= 2 {
element = array[2]
}
vs:
if let element = array[2] {
}
And it would be easy to get the current behavior with a single character:
let element = array[2]!
Comments
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!