Add accessor with bounds check to Array

collection

(Jon Hull) #122

What about "tolerant:"?

let x:Int? = myInts[tolerant: 0]

Also "lax:" is short :slight_smile:

let x:Int? = myInts[lax: 0]

(Andrew Trick) #123

This please:
let x:Int? = myInts[probe: 0]

What fun it is to bikeshed.


(Erik Little) #124

Would it be possible to just move forward a proposal without a defined name, and let the core team decide from the suggested options (or come up with a new one)?

This feels like the kind of thing where we mostly agree on the functionality, but really struggle on the naming.


(Erica Sadun) #125

I've come to the unexpected conclusion over time that I do not want to add a range-checked index. I could not find any use-cases outside of sample code that I need this feature for.

If you can provide some real world, real code use-cases, I would probably swing around back to being "for" instead of "against".


(Xiaodi Wu) #126

I would disagree pretty strongly with this approach. It's the role of the community to grapple with the naming, which is a key part of designing the language.


(Erik Little) #127

I merely say this because of the recent Result proposal. That was one of those cases where the functionality initially presented was revised substantially and immediately put forth for re-review.

I think what we need at this point is a champion to put forward a written proposal and an accompanying implementation.


(Xiaodi Wu) #128

The recent Result proposal went through at least three rounds of design before review. It was put forward for review without a consensus design in no small part because the core team felt it was important to have the process completed for Swift 5, for reasons they outlined. Therefore, it is an aberration strenously to be avoided without good reason, not a precedent.

There are at least two versions of this proposal already written; the implementation isn't going to be a barrier either.



(Daryle Walker) #129

Maybe because if you have some random Index value that you're going to splat on some Collection's subscript without any clue (in advance) if said index value is valid, then your algorithm's design is already fundamentally broken?! (Maybe something like this could be added to the commonly-rejected rationale.)

There is the earlier case by @jawbroken where s/he had to check a cell's neighbors in a multi-dimensional grid, and nil-returns help avoid special-casing the (literal) edge and corner cases, but most uses are probably not like this. Just run a loop from startIndex to endIndex, or vice-versa for bi-directional collections, and sprinkle in break and/or continue when you don't need to visit every element. Use zip or Sequence.enumerated() when you need to track counts.

Due to array slices, an integer index is not necessarily an offset. Maybe we need an alternate subscript to track offsets. I think that has been proposed before too.