[swift-evolution] two protocols with the same method name
θι«
owenzx at gmail.com
Fri Jan 8 20:40:59 CST 2016
Howard is almost right.
Protocol A means the ring variable must have a getter. For the setter, you
can have it or not. Both won't violate the conformance of the Protocol A.
Actually, you can write as
protocol B:A {
var ring:String { get set }
}
The is more for the last line
// (x as A).ring = "π" Error, A doesn't have set
You will find more that
(x as B).ring = "1"
doesn't work either. The reason is that (x as B) is a let by default.
You need do
var b = x as B
b.ring = "1"
Above works.
zhaoxin
On Sat, Jan 9, 2016 at 10:19 AM, Howard Lovatt via swift-evolution <
swift-evolution at swift.org> wrote:
> 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
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
--
Owen Zhao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160109/c0de6e7f/attachment.html>
More information about the swift-evolution
mailing list