I think the subscript index should not be optional - you want 2nd array 
element, 3rd, etc., not a "nil" element - this makes no sense to me. Also it 
feels like some strange optional behavior compared to the normal usage, so -1 
from me on this idea.

I think that there can be situations where you'd like to have an optional value 
from array, e.g. Checking for index of some element before then increasing it 
by one to get the next one. It would be more convenient and look more clearer 
to have one optional binding in if/guard than checking against the count and 
then handling the element. It also feels natural - there either can be an 
element at given subscript, or there might not be - the first thing it brings 
to mind is optionals, also it feels familiar from dictionaries.

I think we should preserve the unsafe version for performance reasons but allow 
some optional access too, so it feels more natural when reading the code. I saw 
several implementations on stack overflow and the one with "safe:" argument 
name seemed quite popular. I think it would add complexity to the language to 
add another syntax/keyword/strange use cases (like optional index), so I'd stay 
with the argument label. Maybe it's not perfectly pretty, but doesn't harm the 
language in any way and its intentions are clear.

This is of course something everyone could write in their own extension, but I 
think adding it to the standard lib would make me happy. I already had some 
cases where I'd like to use it, not sure about others, but this is still up to 
the discussion.

Wysłane z iPhone'a

Dnia 13.04.2017 o godz. 18:18 David Sweeris <[email protected]> napisał(a):

> 
>> On Apr 13, 2017, at 08:53, Josh Parmenter <[email protected]> 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 
> 
> 
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to