<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 15, 2015, at 10:26 AM, Thorsten Seitz &lt;<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""></div><div class=""><br class=""></div><div class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 8:04 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I've had a number of twitter conversations with users who have valiantly fought our type system and lost when trying to make their protocols interact well with non-final classes. A few from recent memory:<div class=""><br class=""></div><div class="">- Rob Napier struggling to implement a `copy` method that works well with subclasses:&nbsp;<a href="https://twitter.com/cocoaphony/status/660914612850843648" class="">https://twitter.com/cocoaphony/status/660914612850843648</a></div><div class="">and making the observation that the interaction of protocol conformance and subtyping is very difficult to teach.</div><div class=""><br class=""></div><div class="">- Matt Bischoff trying to make Cocoa class clusters retroactively conform to his factory protocol:</div><div class=""><a href="https://twitter.com/anandabits/status/664294382774849536" class="">https://twitter.com/anandabits/status/664294382774849536</a></div><div class=""><br class=""></div><div class="">and Karl Adam trying to do the same:</div><div class=""><a href="https://gist.github.com/thekarladam/c3094769cc8c87bf55e3" class="">https://gist.github.com/thekarladam/c3094769cc8c87bf55e3</a></div><div class=""><br class=""></div><div class="">These problems stem from the way protocol conformances currently interact with class inheritance—specifically, that if a class conforms to a protocol, then all of its possible derived classes also conform. This seems like the obvious way things should be, but in practice it ends up fighting how many classes are intended to be used. Often only a base class is intended to be the public interface, and derived classes are only implementation details—Cocoa class clusters are a great example of this. The inheritance of protocol conformances also imposes a bunch of knock-on complexity on conforming classes—initializer requirements must be satisfied by `required` initializers (which then must be overridden in all derived classes, to the pain of anyone touching an NSCoding-inherited class), and methods often must return dynamic `Self` when they'd really prefer to return the base class.</div><div class=""><br class=""></div><div class="">To mitigate these issues, I'd like to float the idea that protocol conformances *not be* inherited by default. If you declare a class as conforming to a protocol, only exactly that class can be bound to a type parameter constrained by that protocol:</div></div></div></blockquote><div class=""><br class=""></div><div class="">What exactly does that mean?</div><div class=""><br class=""></div><div class="">If A conforms to Runcible and B is derived from A that means that B is a subtype of A and can be used everywhere an A can be used (substitution principle).</div><div class="">Therefore if I can use an A where a Runcible is required, I must be able to use a B as well. Subtyping is transitive which gets somehow broken in this proposal (or maybe not, see below).</div></div></div></div></div></div></blockquote><div><br class=""></div><div>B-is-subtype-of-A-is-subtype-of-Runcible still holds, but B couldn't be bound to a type variable that was constrained to be Runcible, only A. Subtyping is never quite cut and dry when interacting with parametric type systems; it's not always the case that Foo&lt;B&gt; is a subtype of Foo&lt;A&gt; even if B is a subtype of A. This adds extra edge cases, definitely.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">protocol Runcible {}</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">class A: Runcible { }</div><div class="">class B : A { }</div></blockquote></div></div></blockquote></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">func foo&lt;T: Runcible&gt;(x: T) {}</div><div class=""><br class=""></div><div class="">foo(B()) // calls foo with T == A</div></blockquote></div></div></blockquote><div class=""><br class=""></div><div class="">This would just hold for the static type of T, right?</div><div class="">The dynamic type of x still stays B and dynamic dispatch would respect that whereas static dispatch (e.g. for extension methods declared for A and B separately) would use the static type A (i.e. dispatch to the extension method declared for A).</div></div></div></div></div></div></blockquote><div><br class=""></div><div>Right, the dynamic type of the variable 'x' would still be B, even though T == A.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Since subclasses are still subtypes of the base class, in many cases client code won't have to change at all, since derived instances can implicitly upconvert to their conforming base class when used in protocol types or generics that only the base class conforms to. (There are cases like if the type parameter appears in a NonCovariant&lt;T&gt; type where this isn't possible, though.) </div></div></div></blockquote><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Protocol requirements for a non-inherited conformance don't need to be `required` initializers, or maintain covariant returns:</div></div></div></blockquote><div class=""><br class=""></div>The required initializers are probably the motivation for this change.</div><div class="">Is there a problem with covariant returns as well? I'd think a subclass does not have to do anything to just get them, so I don't see a problem there.</div><div class=""><br class=""></div><div class="">If I understand the desire behind the proposal correctly, it is about</div><div class="">(a) stopping Self types to vary below some point in the hierarchy</div><div class="">(b) controlling the requiredness of initializers within the hierarchy (doesn’t that collide with the idea of requiring an initializer in the first place?)</div><div class=""><br class=""></div><div class="">I have the vague felling (or hope) that this can be done more nicely in other ways without tampering with subtyping.</div><div class=""><br class=""></div><div class="">Joe Groff recently proposed in another thread something similar for Self types in Equatable:</div><div class=""><br class=""></div><div class="">public&nbsp;protocol&nbsp;Equatable {<br class="">&nbsp; &nbsp;&nbsp;typealias&nbsp;EquatesWith =&nbsp;Self&nbsp;where&nbsp;Self: EquatesWith<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;==(lhs:&nbsp;EquatesWith, rhs:&nbsp;EquatesWith) -&gt;&nbsp;Bool<br class="">}<br class=""><br class=""></div><div class="">This way subclasses or subprotocols can decide where they want to fix EquatesWith to a fixed value:</div><div class=""><br class=""></div><div class="">protocol Fungible {</div><div class="">&nbsp; &nbsp; typealias F = Self where Self: F</div><div class="">&nbsp; &nbsp; static func funged() -&gt; F</div><div class="">}</div><div class=""><br class=""></div><div class="">class C: Fungible {</div><div class="">&nbsp; &nbsp; class func funged() -&gt; F { return C() }</div><div class="">}</div><div class=""><br class=""></div><div class="">class D: C {</div><div class="">&nbsp; &nbsp; override class func funged() -&gt; F { return D() }</div><div class="">}</div><div class=""><br class=""></div><div class="">class X: C {</div><div class="">&nbsp; &nbsp; typealias F = C</div><div class="">}</div><div class=""><br class=""></div><div class="">D.funged() has static type D</div><div class="">X.funged() has static type C</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Would this fit your expectations for the Self type issues? Or have I misunderstood the problem completely?</div></div></div></div></div></blockquote><div><br class=""></div><div>Equatable has the opposite problem that its Self requirements are contravariant, since they appear in argument position; this means Equatable itself can't naturally be used as the existential type of all Equatables, but it isn't a problem for a class hierarchy to conform all subclasses to Equatable—`Base == Base` satisfies the protocol requirement for any possible Derived type. An associated type indirection like this still wouldn't solve the factory method covariance problem, since `Self` inside protocol conformances today always refers to the potentially-derived type when classes are involved.</div><div><br class=""></div><div>-Joe</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><br class=""></div><div class="">Maybe something similar can be done for requiredness of initializers.</div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">protocol Fungible {</div><div class="">&nbsp; init()</div><div class="">&nbsp; static func funged() -&gt; Self</div><div class="">}</div><div class=""><br class=""></div><div class="">class C: Fungible {</div><div class="">&nbsp; init() {} // Non-required init is fine, since subclasses aren't directly Fungible</div><div class=""><br class=""></div><div class="">&nbsp; // Non-Self return is fine too</div><div class="">&nbsp; class func funged() -&gt; C { return C() }</div><div class="">}</div><div class=""><br class=""></div></blockquote>An individual subclass that wanted to refine the conformance could do so by `override`-ing it, and providing any necessary covariant overrides of initializers and methods:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">class D: C, override Fungible {</div><div class="">&nbsp; // D must provide its own init()</div><div class="">&nbsp; init() { super.init() }</div><div class=""><br class=""></div><div class="">&nbsp; // D must override funged() to return D instead of C</div><div class="">&nbsp; override class func funged() -&gt; D { return D() }</div><div class="">}</div><div class=""><br class=""></div></blockquote>And if a class hierarchy really wants to impose a conformance on all possible subclasses, as happens today, we could let you opt in to that:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">class E: required Fungible {</div><div class="">&nbsp; // init() must be required of all subclasses</div><div class="">&nbsp; required init() { }</div><div class=""><br class=""></div><div class="">&nbsp; // funged() must return a covariant object</div><div class="">&nbsp; class func funged() -&gt; Self { return Self() }</div><div class="">}</div><div class=""><br class=""></div></blockquote>This is undoubtedly a complication of the language, but I think it might let us more accurately model a lot of things people seem to want to do in practice with class hierarchies and protocols, and it simplifies the behavior of the arguably common case where inheritance of the conformance isn't desired. What do you all think?<div class=""><br class=""></div><div class="">-Joe</div></div></div></blockquote></div></div></div></div></div></blockquote></div><br class=""></body></html>