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

Howard Lovatt howard.lovatt at gmail.com
Fri Jan 8 20:19:12 CST 2016


I don’t really get what you are driving at. If I rework your example so that it runs:

//πŸ’
protocol A {
    var ring: String { get }
}

//πŸ””
protocol B {
    var ring: String { get set }
}

class X: A, B {
    var _ring = "πŸ’"
    var ring: String {
        get {
            return _ring
        }
        set {
            self._ring = newValue
        }
    }
}
let x = X()
(x as A).ring // πŸ’
(x as B).ring // πŸ’
x.ring // πŸ’
x.ring = "πŸ””"
x.ring // πŸ””
// (x as A).ring = "πŸ””" Error, A doesn't have set

Then the behaviour is exactly what I would expect.

> On 7 Jan 2016, at 9:18 PM, 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160109/573d2b71/attachment.html>


More information about the swift-evolution mailing list