[swift-evolution] Arrays Returning Optionals instead of Index Out of Bounds

Christopher Kornher ckornher at me.com
Mon Jun 6 12:44:42 CDT 2016


The proposal needs to discuss arrays of optionals  e.g.:  ```Array<Int?>```
These are not just legal, they are quite handy as well.

Dictionaries can also be declared with optional values, of course: ```Dictionary<Int,Int?>```
These can be pretty confusing to deal with and we should not add to confusion with arrays.

An example of the confusion:

var d = Dictionary<Int,Int?>
...
d[0] = nil		// Does this delete the element or set it to nil? Honestly, I forget. The point is, the syntax is ambiguous.

It would be nice to create a completely unambiguous syntax for collections with optional values.

- Chris K


> On Jun 6, 2016, at 11:10 AM, Vladimir.S via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Please find this draft of proposal(hope this is correct link for latest version):
> https://github.com/luish/swift-evolution/blob/more-lenient-subscripts/proposals/nnnn-more-lenient-collections-subscripts.md
> 
> The main idea is to introduce 2 new subscript methods:  [clamping:] and [checking:]
> 
> There was discussion in "[Proposal] More lenient subscript methods over Collections" and (older) "[Proposal] Safer half-open range operator"
> 
> On 06.06.2016 19:50, Rob Norback via swift-evolution wrote:
>> Hi Everyone!
>> 
>> This is my first time emailing the swift evolution list, so if this topic
>> has already been discussed please let me know.  I looked through all the
>> accepted and rejected proposals and it doesn't seem to be on there.
>> 
>> The main thought is that dictionaries return optionals, so why not arrays?
>> Or other CollectionTypes for that matter.  I would think this would be the
>> expected behavior in this situation:
>> 
>> var myArray:[String?] = []
>> print(myArray[4])
>> // EXC_BAD_INSTRUCTION, but could just be Optional(nil)
>> 
>> Then you could do things like
>> 
>> if let arrayValue = myArray[4] {
>>   // do something
>> }
>> 
>> Of course you could simply check with with the count, but considering
>> Swift's use of optionals to represent empty variables rather than erroring
>> out or returning an empty String, I think this functionality would be
>> appropriate to include in the Swift standard library.
>> 
>> And there's about 15,000 people who've looked for this functionality in the
>> last year:
>> http://stackoverflow.com/questions/25329186/safe-bounds-checked-array-lookup-in-swift-through-optional-bindings.
>> 
>> Please let me know what you think.
>> 
>> Best,
>> Rob Norback
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list