swiftlang/swift

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

Open

#46,347 创建于 2017年1月27日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Swift (69,989 star) (10,719 fork)batch import
affects ABIgood first issueimprovementstandard libraryswift evolution proposal needed

描述

Previous ID SR-3762
Radar rdar://problem/26476285
Original Reporter @airspeedswift
Type Improvement
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.

贡献者指南