<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 28, 2015, at 12:02 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On Dec 28, 2015, at 8:49 AM, Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I have brought up the idea of a non-covarying Self a few times. &nbsp;<br class=""><br class="">I was surprised to realize that Self is actually non-covarying when used for parameters in protocol declarations!<br class=""><br class="">Here is an example demonstrating this:<br class=""><br class="">protocol P {<br class="">&nbsp;&nbsp;func foo(s: Self)<br class="">}<br class="">protocol Q {<br class="">&nbsp;&nbsp;func bar() -&gt; Self<br class="">}<br class=""><br class="">class C: P {<br class="">&nbsp;&nbsp;// this works! &nbsp;Self as an argument type in the protocol declaration does not covary<br class="">&nbsp;&nbsp;func foo(c: C) {}<br class="">}<br class=""><br class="">class D: C {}<br class=""><br class="">extension C: Q {<br class="">&nbsp;&nbsp;// method ‘bar()’ in non-final class ‘C’ must return ‘Self’ to conform to protocol ‘Q'<br class="">&nbsp;&nbsp;func bar() -&gt; C { return self }<span class="Apple-converted-space">&nbsp;</span><br class="">}<br class=""><br class=""><br class="">It doesn’t make sense to allow a co-varying Self for parameters so I can understand how the current state might have arisen. &nbsp;At the same time, using Self to mean two different things is inconsistent, confusing and it doesn’t allow us to specify a non-covarying Self as a return type in protocol requirements. &nbsp;<br class=""><br class="">As I have pointed out before, the ability to specify a non-covarying Self as a return type would make it possible to design a protocol that can be retroactively conformed to by non-final classes (such as those in Apple’s frameworks).<br class=""><br class="">I think it would be a very good idea to introduce a non-covarying Self which would specify the type that adds conformance to the protocol and require this Self to be used in places where covariance is not possible, such as parameter types. &nbsp;It would also be allowed elsewhere, such as return types, making it easier to conform non-final classes when covariance is not required by the protocol.<br class=""><br class="">One possible name is `ConformingSelf`. &nbsp;One thing I like about this name is that it makes it very clear that it is the type that introduces protocol conformance.<br class=""><br class="">I’m interested in hearing thoughts on this.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I proposed this a while back — see "controlling protocol conformance inheritance" from a while back. The current rules preserve inheritability of the protocol conformance in all cases. `Self` in argument positions maps to the root class of the conformance, since a method taking `Base` can also take any `Derived` class and thereby satisfy the conformance for `Derived`. In return positions, the derived type must be produced. I think there's value in controlling this behavior, but the control belongs on the conformer's side, not the protocol's. For some class hierarchies, the subclasses are intended to be API themselves in order to extend behavior. Every UIView subclass is interesting independently, for example, and ought to satisfy `NSCoding` and other requirements independently. In other class hierarchies, the base class is intended to be the common API, and subclasses are just implementation details. NSString, NSURL, etc. exemplify this—for most purposes the common `NSCoding` implementation is sufficient for all NSStrings. Likewise, you probably want NSURL to conform to `StringLiteralConvertible` but don't particularly care what subclass you get out of the deal. I had proposed the idea of modifying a class's conformance declaration to allow it control whether the conformance is inherited:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">extension NSString: static NSCoding { } // NSCoding conformance statically applies to only NSString, Self == NSString</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">extension UIView: required NSCoding { } // NSCoding conformance is required of all subclasses, Self &lt;= UIView</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></blockquote><br class=""></div><div>If I understand correctly, what you’re saying is that you don’t think there is a reason why a protocol would want to specifically require invariance. &nbsp;I would have to think about this some more but you may well be right. &nbsp;The conforming-side solution would definitely work in every use case I know of.</div><div><br class=""></div><div>I figured out how Self protocol requirements are actually treated consistently from a particular perspective. &nbsp;Self is effectively treated as a covariant requirement, but becomes an invariant requirement when used in positions where covariance is not possible (parameters and return type for structs and final classes). &nbsp;This makes sense and is consistent, if not totally obvious. &nbsp;Is this reasonably accurate?</div><div><br class=""></div><div>From that perspective, it seems Self should also behave this way when used as a return type in method signatures:</div><div><br class=""></div><div>protocol&nbsp;P {<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;bar() -&gt;&nbsp;Self<br class="">}<br class="">final&nbsp;class&nbsp;C:&nbsp;P&nbsp;{</div><div>&nbsp; &nbsp; // this doesn’t work, but maybe it should because C is final and can’t covary:<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;bar() -&gt;&nbsp;Self&nbsp;{&nbsp;return&nbsp;C() }</div><div><br class=""></div><div>&nbsp; &nbsp; // this works:</div><div>&nbsp; &nbsp; // func&nbsp;bar() -&gt;&nbsp;C&nbsp;{&nbsp;return&nbsp;C() }<br class="">}<br class=""><br class=""></div><div>struct&nbsp;S:&nbsp;P&nbsp;{</div><div>&nbsp; &nbsp; // this doesn’t work, but maybe it should because S is a struct and can’t covary:<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;bar() -&gt;&nbsp;Self&nbsp;{&nbsp;return&nbsp;S() }</div><div><div><br class=""></div><div>&nbsp; &nbsp; // this works:</div><div>&nbsp; &nbsp; // func&nbsp;bar() -&gt;&nbsp;S&nbsp;{&nbsp;return&nbsp;S() }</div>}<br class=""><br class=""></div><div>This thread was prompted by work on a proposal for protocol forwarding which requires careful consideration of Self requirements. &nbsp;</div><div><br class=""></div><div>One of the things I have considered is whether it would be possible and desirable to wrap and forward the entire interface of a type rather than just specific protocols. &nbsp;As it turns out, this is not possible in a robust manner without using Self in the signature where the type should be promoted to the forwarding type (i.e. just because a method on Double takes a Double parameter you don’t necessarily want to promote the type of that parameter to Kilograms when wrapping Double).</div><div><br class=""></div><div>Matthew</div><br class=""></body></html>