SE-0204: Add last(where:) and lastIndex(where:) methods; rename index(of:) and index(where:) methods

Proposal Update

The core team discussed this today, and we've decided to revise this proposal by including the rename of index(of:) to firstIndex(of:) and index(where:) to firstIndex(where:), as described in the alternatives section of the proposal.

Renaming functions must meet a fairly high bar, but we felt that this rename is worth considering because:

  • It firmly establishes first... and last... as consistently-named families of analogous functions.
  • It leaves index... to be just the index-manipulation functions such as index(after:), which do not access the elements of the collection.

That is, it creates or reinforces a number of general patterns in the naming of functions on Collection.

Accordingly, the proposal will be revised to include these renames, and the review period has been extended to next Tuesday, April 10th.

22 Likes

+1 for the proposal and the new update with the rename. This line of thinking I believe is a great way for Swift to have family of related logic to can be easily discovered. It would make future things like .firstRandom() more likely and a better fit.
Thank you,
Cheyo

I'm very much +1 to this proposal and of the renaming that has been introduced. These are fairly straightforward methods that are of value. I also think that the renaming introduces consistency, which is always a good thing (usually).

Good proposal before, and even better now. +1!

1 Like

Yes I understand that (see my third code example).

Sorry I wasn’t making myself clear, I mean Int subscript.

My point was the indices property is hard to discover therefore lead to more potentially wrong and inefficient code using index(of:).

Any more thoughts about this? And in particular, about the change to include renaming index(of:) and index(where:)?

Nate has updated the proposal.

+1, and I like the idea of cleaning up the "families" of methods on Collection.

+1 as well, very useful and well named. Also great that we’re fixing the old names too, I remember double-checking docs to be sure it’s the first index, etc.