[swift-evolution] two protocols with the same method name

davesweeris at mac.com davesweeris at mac.com
Thu Jan 7 20:36:10 CST 2016


I do the 2nd case on purpose. Usually it’s to refine typealiases rather than for methods/variables, but I’m pretty sure I’ll be dealing with those as well fairly soon.

> On Jan 7, 2016, at 17:40, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Mm. There are two cases here:
> 
> - Two protocols use the same method name for completely unrelated things.
> - Two protocols use the same method name for similar things.
> 
> In the latter case, having one method satisfy both requirements is probably fine. In the former, there would definitely be a problem…but I have a hard time thinking of a type that should actually conform to two protocols that do completely different things and yet manage to use the same name for something. Do you have a real-life example where this has been a problem? Everything I can find online looks contrived.
> 
> Jordan
> 
> 
>> On Jan 7, 2016, at 2:18, Grzegorz Leszek via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> I suggest compile warning if one classes/structs/enums implements
>> protocols with the same name.
>> It could lead to confusions, when methods of those protocols will mean
>> different things.
>> It will force to implement parent protocol with shared methods or
>> change method in one of the protocols.
>> Below is an example.
>> Regards,
>> Greg
>> 
>> //💍
>> protocol A {
>> var ring: String { get }
>> }
>> 
>> //🔔
>> protocol B {
>> var ring: String { get set }
>> }
>> 
>> class X: A, B {
>> var ring: String {
>>   get {
>>     return "💍"
>>   }
>>   set {
>>     self.ring = newValue
>>   }
>> }
>> }
>> let x = X()
>> let somewhereInTheProject = "\(x.ring) the bell"
>> x.ring = "🔔" // ERROR!
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list