[swift-evolution] [Pitch] Adding safety to arrays

David Sweeris davesweeris at mac.com
Thu Apr 13 11:18:54 CDT 2017


> On Apr 13, 2017, at 08:53, Josh Parmenter <jparmenter at vectorform.com> wrote:
> 
> This seems inconsistent to me. 2 is 2... 2 itself is not optional. You wouldn't expect 2 to be unwrapped.

Correct. I think the idea was that "2?" would get converted to an `Optional<Int>`, which would then call a subscript that took an `Index?` instead of `Index`. The trailing "?" doesn't do that, but for some reason I thought it might.

IMHO, the semantics are clear(ish) in context, but it feels slightly odd for "?" to have two opposite behaviors depending on whether the argument is or isn't an Optional.

In any case, given that we're discussing a "safe subscript", we need a way to differentiate the safe subscript from the unsafe subscript. The only two ways to do that are to either add an argument label or change the argument type, and IMHO the best alternate argument type is `Index?`, since it's pretty light-weight, already part of the stdlib, and already familiar to Swift developers.

Someone already said it was a bad idea, though, so I'm rethinking my support.

- Dave Sweeris 



More information about the swift-evolution mailing list