swiftlang/swift

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

開放

#46,347 建立於 2017年1月27日

 (1 則留言) (0 個反應) (0 位負責人)Swift (10,719 個分叉)batch import
affects ABIgood first issueimprovementstandard libraryswift evolution proposal needed

倉庫指標

星標
 (69,989 顆星)
PR 合併指標
 (平均合併 8天 17小時) (30 天內合併 510 個 PR)

描述

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.

貢獻者指南