<div dir="ltr">A protocol X could fulfill conformance to a protocol Y.<div><br></div><div><div><font face="monospace, monospace">protocol SomeProtocol: Equatable {}</font></div><div><font face="monospace, monospace">enum SomeType1: SomeProtocol { case Case }</font></div><div><font face="monospace, monospace">enum SomeType2: SomeProtocol { case Case }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">func == (a: SomeProtocol, b: SomeProtocol) -> Bool {</font><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace">// should be allowed to use protocol directly</span></div><div><font face="monospace, monospace"><span style="white-space:pre"> </span>switch (a, b) {</font></div><div><font face="monospace, monospace"><span style="white-space:pre"> </span>case let (a, b) as (SomeType1, SomeType1): return a == b</font></div><div><font face="monospace, monospace"><span style="white-space:pre"> </span>case let (a, b) as (SomeType2, SomeType2): return a == b</font></div><div><font face="monospace, monospace"><span style="white-space:pre"> </span>default: return false</font></div><div><font face="monospace, monospace"><span style="white-space:pre"> </span>}</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">// Equatable requirement is fulfilled for SomeProtocol</font></div><div><font face="monospace, monospace">let a: SomeProtocol = SomeType1.Case // should be allowed to use protocol directly</font></div><div><font face="monospace, monospace">let b: SomeProtocol = SomeType2.Case</font></div><div><font face="monospace, monospace">print(a == b) // should print "false"</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">var array = [SomeProtocol]()</font><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace">// should be allowed to use protocol directly</span></div><div><br></div><div>That kind of conformance fulfillment should not be inherited though or else this could cause a recursion, i.e. types implementing the protocol must still conform to<br></div><div><br></div><div>Since <font face="monospace, monospace">SomeProtocol</font> has internal visibility all types and protocols explicitly declaring conformance are even known at compile-time.</div><div>For that reason implementing the equality operator for the protocol wouldn't even be necessary. The compiler does already know that all types conforming to the protocol are equatable and could even generate the equality operator.</div><div><br></div><div><br></div><div>I find the suggestion from Paul Cantrell very useful where you can use all properties and methods of the protocol which do not depend on an associated type or Self.</div><div><br></div><div><font face="monospace, monospace">var array = [Hashable]()<br></font></div><div><font face="monospace, monospace">// …</font></div><div><font face="monospace, monospace">for element in array {</font></div><div><font face="monospace, monospace"> print(element.hashValue)</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div>Who cares that it doesn't fulfill Equatable conformance yet?</div><div><br></div>
</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 14, 2015 at 8:40 AM, ilya via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You can achieve the same result more cleanly with <div><br></div><div><div>func maxY<T:P, U:P>(p1:T, p2: U) -> Int {</div><span class=""><div> return max(p1.y, p2.y) // No problems here!</div><div>}</div></span></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Mon, Dec 14, 2015 at 10:03 AM, Paul Cantrell via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div class="h5"><div><div><span><blockquote type="cite"><div>On Dec 13, 2015, at 11:56 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Dec 13, 2015, at 3:55 PM, Marc Knaup via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div></blockquote></div></div></div></blockquote></span><span><blockquote type="cite" style="font-family:HelveticaNeue"><div><div dir="ltr"><div><blockquote type="cite"><blockquote type="cite">Is there any info yet if and how we will be able to refer to instances of protocols that have associated types?</blockquote></blockquote></div></div></div></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><div style="font-family:HelveticaNeue"><blockquote type="cite"><blockquote type="cite">As far as I know, this isn't a solvable problem.</blockquote></blockquote></div></span></div><span><div><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote></div><blockquote type="cite"><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div dir="ltr"><div>What is the difficulty in supporting this?</div></div></div></blockquote><br></div><div>Here's a simple example:</div><div><br></div><div>protocol P {</div><div> typealias A</div><div> var x: A { get set }</div><div>}</div><div><br></div><div>struct Y : P {</div><div> var x: Int</div><div>}</div><div><br></div><div>struct Z : P {</div><div> var x: String</div><div>}</div><div><br></div><div>func f(p1: P, p2: P) {</div><div> p1.x = p2.x // assigning an Int to a String?</div><div>}</div></div></blockquote><br></span></div><div>p1.x = p2.x should be a compiler error, because there’s not enough type information. But that shouldn’t stop a programmer from doing this:</div><div><br></div><div><span><div style="word-wrap:break-word"><div>protocol P {</div><div> typealias A</div><div> var x: A { get set }</div></div></span><div> var y: Int</div><span><div style="word-wrap:break-word"><div>}</div><div><br></div><div>struct Y : P {</div><div> var x: Int</div></div></span><div style="word-wrap:break-word"><div> var y: Int</div></div><span><div><div style="word-wrap:break-word"></div></div><div style="word-wrap:break-word"><div>}</div><div><br></div><div>struct Z : P {</div><div> var x: String</div></div></span><div> var y: Int<br></div><div style="word-wrap:break-word"><div>}</div><div><br></div></div><div style="word-wrap:break-word"><div>func maxY(p1: P, p2: P) -> Int {</div><div> return max(p1.y, p2.y) // No problems here!</div><div>}</div></div><div><div style="word-wrap:break-word"><div><br></div><div>…right?</div><div><br></div><div>Cheers, P</div><div><br></div></div></div></div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54Pf29ZeGQ869EfUElXAJH5tAK8E-2BcWCI4TUtgPPpNaQX-2FabK11JK6glNFNFK4KSp-2FSIrBBCG02Kv3IlmfFCpoqNsEx28po3nr67qV5BoVsARirFr4Kl8j3zN5KJ2RKi0EvIQqTBBtA3u7oTQdhFokN4bPCoh5WIsSzR9V5lPR4icuceJOIFEzOymDgAR5CGrao-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" target="_blank">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>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=6ZGE61OxINd5lLe2xYh9Ku-2BXbixWNr2nvfzp2IB1sZjb0N2dnFJHaqQr1LZlgz7Rgah3R1OQsC1pKOaTaVmYHPgtheswPjFH9YxPuMeoIDij5ieOmJGauREICocp4FialDi0O1bj4ABS1xQHrVi35RwAK2OAZGzE8TlVnKi2cVP6vhJ0af4l4mRw1EhNEfXabhNjFHxlYKaKXDn4fKqoUFWO5JbI5d3pQDrc-2FvIVGS4-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">
<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>