two protocols with the same method name

I have good news! This syntax was implemented at some point in the last 5 years!

protocol A {
    var foo: Int {get}
}
protocol B {
    var foo: Int {get}
}
struct C: A, B {
    @_implements(A, foo)
    var fooForA: Int
    @_implements(B, foo)
    var fooForB: Int
}
let c = C(fooForA: 1, fooForB: 2)
let ca: A = c
let cb: B = c
// print(c.foo) // error: ambiguous use of 'fooForB'
print(ca.foo) // 1
print(cb.foo) // 2

Unfortunately it is underscored, which means it's not officially supported. There may be some unsupported edge cases, it can go away after we have something official, your warranty is void.

It is a good starting point for proposing something official

8 Likes