[swift-evolution] two protocols with the same method name
Brent Royal-Gordon
brent at architechies.com
Sun Jan 10 16:51:16 CST 2016
> By giving warning simply for same name, it will be quite annoying when the project run into this situation without any wrong. For example:
>
> protocol ForwardIndexType : _Incrementable {
> @warn_unused_result
> public func advancedBy(n: Self.Distance) -> Self
> }
>
> extension ForwardIndexType {
> @warn_unused_result
> public func advancedBy(n: Self.Distance) -> Self
> @warn_unused_result
> public func advancedBy(n: Self.Distance, limit: Self) -> Self
> @warn_unused_result
> public func distanceTo(end: Self) -> Self.Distance
> }
>
> protocol BidirectionalIndexType : ForwardIndexType
> extension BidirectionalIndexType {
> @warn_unused_result
> public func advancedBy(n: Self.Distance) -> Self
> @warn_unused_result
> public func advancedBy(n: Self.Distance, limit: Self) -> Self
> }
Firstly, for methods and subscriptors the "name" would actually encompass the entire signature, so `advancedBy(_:)` and `advancedBy(_:limit:)` would not conflict because they have different signatures.
Secondly, `ForwardIndexType` and `BidirectionalIndexType` are *not* unrelated protocols—one of them conforms to the other. Thus, we can assume that `BidirectionalIndexType` knows about `ForwardIndexType`'s `advancedBy` methods and intends for its versions to have compatible semantics.
If instead `BidirectionalIndexType` did *not* conform to `ForwardIndexType`, and `RandomAccessIndexType` tried to conform to both `ForwardIndexType` and `BidirectionalIndexType`, *then* we would get an error, because two independent protocols would have declared `advancedBy(_: Self.Distance) -> Self` methods and it's possible they meant for them to have different semantics.
--
Brent Royal-Gordon
Architechies
More information about the swift-evolution
mailing list