Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3762] Default implementation of index(after:) for Integer indices #46347

Open
airspeedswift opened this issue Jan 27, 2017 · 1 comment
Open
Labels
affects ABI Flag: Affects ABI good first issue Good for newcomers improvement standard library Area: Standard library umbrella swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@airspeedswift
Copy link
Member

Previous ID SR-3762
Radar rdar://problem/26476285
Original Reporter @airspeedswift
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement, AffectsABI, StarterProposal
Assignee None
Priority Medium

md5: 0a15e4ebe05ad3320d3ce0d815461616

Issue Description:

There should be a default implementation for Collections whose Index type is Int. This make it more convenient to write integer indexed collections. In the previous collections model it was just understood that an Int index’s successor was the integer + 1 and there was no boilerplate a developer had to write.

extension Collection where Index: Integer {
public func index(after i: Index) -> Index {
return i + 1
}
}

Note, this would mean that collections that did not want this behavior (say, they used a non-unit stride) would need to remember to override this. However, since users can just do integer calculations themselves without using the collection, it would probably be wise for these types to create an opaque index type to wrap the integer in anyway.

This would need an evolution proposal. Part of the initial pitch should include whether this should be done to just Collection or be specific to RandomAccessCollection.

@natecook1000
Copy link
Member

This more or less already exists for RandomAccessCollection—the constraints on the default implementations are Index : Strideable, Index.Stride == IndexDistance, Indices == CountableRange<Index>. The last one of those adds the constraint that Index.Stride is an integer, which seems like what we would want to specify rather than the index itself.

Are there collections that could use an integer for their index but couldn't be random access? Or could/should use a non-unit stride? I think if startIndex..<endIndex turns into a countable range, there should probably be a valid element at each index.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal good first issue Good for newcomers and removed StarterProposal labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects ABI Flag: Affects ABI good first issue Good for newcomers improvement standard library Area: Standard library umbrella swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

3 participants