tera
September 6, 2022, 2:53pm
1
In the following example:
class Base {}
class Derived: Base {}
class C {
func foo(_ v: Base) {}
func bar() -> Derived { Derived() }
}
I can call C's foo
with instances of Derived
:
let c = C()
c.foo(Derived())
and I can treat C's bar
as returning instances of Base
:
let v: Base = c.bar()
yet if I try to formally expressed these abilities in a form of protocol:
protocol P: AnyObject {
func foo(_ v: Derived)
func bar() -> Base
}
I can't conform C to this protocol:
extension C: P {} // π Type 'C' does not conform to protocol 'P'
apparently because protocol conformance requires exact type matching (unlike the co/contra variance flexibility allowed in subclass methods overrides).
The question is: shouldn't swift allow it?
1 Like
Jumhyn
(Frederick Kellison-Linn)
September 6, 2022, 5:25pm
2
Old (but relevant) thread from a couple years back:
Hello Swift community,
I have been working on a "mini-manifesto" whose aim is to set out basic design principles around protocol witness matching and get some agreement from the Swift community on it. One of the reasons why is because I am currently pitching a new form of witness matching and while we've had some discussion on this forum on different kinds of matching (like subtyping, etc), it would be great to have a small document which people can refer to during discussions and use it to creβ¦
1 Like
This reminded me it would be good to revive it again (seems like my PR got closed bc I had to recreate the fork) and hopefully get it merged.
2 Likes
Yes I created a new thread here a while back - Protocol Witness Matching Roadmap
2 Likes