[swift-evolution] [RFC] New collections model: collections advance indices

Brent Royal-Gordon brent at architechies.com
Wed Mar 2 03:08:56 CST 2016


> We would like to propose a major change to how collection indices
> work.  The standard library team has discussed this idea internally
> and we wrote a prototype.  Now we think it is a viable direction to
> consider, and we are bringing it for wider public discussion.

This is super-interesting, and overall I think it's probably an improvement (although I haven't gone *really* deep with generic CollectionType code very often).

I do have a question, though it may simply betray my ignorance. `advance` has two forms:

	func advance(i: Index, by n: IndexDistance) -> Index
	func advance(i: Index, by n: IndexDistance, limit: Index) -> Index

This is a pretty straight port of the old `advance`:

	public func advancedBy(amount: Distance) -> Self
	public func advancedBy(amount: Distance, limit: Self) -> Self

But I'm not sure the `limit` parameter is still appropriate. I've always figured that you were supposed to pass the collection's `endIndex` there, but now that `advance` *is* a method on the collection, it can access that itself. Would we be better served with something like this?

	func unsafeAdvance(i: Index, by n: IndexDistance) -> Index		// no endIndex check
	func advance(i: Index, by n: IndexDistance) -> Index			// endIndex check

Or do you imagine the `limit` parameter being put to some other use that I'm not thinking of?



-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list