<div dir="ltr">I think that this is a good idea overall. I agree that `required override` might necessary as well. <div><br></div><div>---</div><div>TJ</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 7:34 AM, Joe Groff via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I&#39;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><br></div><div>- Rob Napier struggling to implement a `copy` method that works well with subclasses: <a href="https://twitter.com/cocoaphony/status/660914612850843648" target="_blank">https://twitter.com/cocoaphony/status/660914612850843648</a></div><div>and making the observation that the interaction of protocol conformance and subtyping is very difficult to teach.</div><div><br></div><div>- Matt Bischoff trying to make Cocoa class clusters retroactively conform to his factory protocol:</div><div><a href="https://twitter.com/anandabits/status/664294382774849536" target="_blank">https://twitter.com/anandabits/status/664294382774849536</a></div><div><br></div><div>and Karl Adam trying to do the same:</div><div><a href="https://gist.github.com/thekarladam/c3094769cc8c87bf55e3" target="_blank">https://gist.github.com/thekarladam/c3094769cc8c87bf55e3</a></div><div><br></div><div>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&#39;d really prefer to return the base class.</div><div><br></div><div>To mitigate these issues, I&#39;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><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>protocol Runcible {}</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>class A: Runcible { }</div><div>class B { }</div><div><br></div><div>func foo&lt;T: Runcible&gt;(x: T) {}</div><div><br></div><div>foo(B()) // calls foo with T == A</div><div><br></div></blockquote><div>Since subclasses are still subtypes of the base class, in many cases client code won&#39;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&#39;t possible, though.) Protocol requirements for a non-inherited conformance don&#39;t need to be `required` initializers, or maintain covariant returns:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>protocol Fungible {</div><div>  init()</div><div>  static func funged() -&gt; Self</div><div>}</div><div><br></div><div>class C: Fungible {</div><div>  init() {} // Non-required init is fine, since subclasses aren&#39;t directly Fungible</div><div><br></div><div>  // Non-Self return is fine too</div><div>  class func funged() -&gt; C { return C() }</div><div>}</div><div><br></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><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>class D: C, override Fungible {</div><div>  // D must provide its own init()</div><div>  init() { super.init() }</div><div><br></div><div>  // D must override funged() to return D instead of C</div><div>  override class func funged() -&gt; D { return D() }</div><div>}</div><div><br></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><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>class E: required Fungible {</div><div>  // init() must be required of all subclasses</div><div>  required init() { }</div><div><br></div><div>  // funged() must return a covariant object</div><div>  class func funged() -&gt; Self { return Self() }</div><div>}</div><div><br></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&#39;t desired. What do you all think?<div><br></div><div>-Joe</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCq6GE-2BEsvtz7fmBm6JJGkhLcuaQBBrj5tn5uxBZzIWmkOOVOeTt56jNiCsB0V3nLfj84oPV2ht4-2FO7Yo9LLY8E7qIO-2B6w-2BXvWUiVnKVycf6MxZOwBnxMmV8U00PlnheW8sydrc6G1W5jBrO62EplZGc5mDxDuqVS1DDE7EnLdTGPs24PX2wVC-2FkGIlr2c2L-2F28-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>