In the Swift standard library, we have Strideable proto incorporated two methods:
func distance(to other: Self) -> Self.Stride
func advanced(by n: Self.Stride) -> Self
It would be natural to have defining operators in a like manner:
extension Strideable {
static func +(lhs: Self, rhs: Self.Stride) -> Self {
return lhs.advanced(by: rhs)
}
static func -(lhs: Self, rhs: Self.Stride) -> Self {
return lhs.advanced(by: -rhs)
}
static func -(lhs: Self, rhs: Self) -> Self.Stride {
return lhs.distance(to: rhs)
}
}
Unfortunately, this is true only for pointers (UnsafePointer, UnsafeRawPointer, ...) which conform _Pointer protocol implicitly, which inherits from Stridable.
Assumption for restricted declaration looks like to avoid conflicts in some parts of code. Does anybody know the true reason for not defining these methods for all conformed Strideable?