<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Note that it would be sound for the language to allow the opposite, which is called “contravariance” (the more specific type takes a more general input):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: #323e7d" class="">protocol</span> A {}</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: #323e7d" class="">protocol</span> B: <span style="color: #587ea8" class="">A</span> {}</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(50, 62, 125);" class=""><span style="color: #000000" class=""> </span>protocol<span style="color: #000000" class=""> C {</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: #323e7d" class="">func</span> test(x: <span style="color: #587ea8" class="">B</span>) <span style="color: #668b49" class="">// was A</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: #323e7d" class="">class</span> M: <span style="color: #587ea8" class="">C</span> {</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: #323e7d" class="">func</span> test(x: <span style="color: #587ea8" class="">A</span>) {} <span style="color: #668b49" class="">// was B</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> }</div><div class=""><br class=""></div></div></div>It could also allow covariant return types (the more specific type returns a more specific output):<br class=""><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(50, 62, 125); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>protocol<span style="color: #000000" class=""> C {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">func</span> test() -> <span style="color: #587ea8" class="">A</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">class</span> <span style="text-decoration: underline" class="">M</span>: <span style="color: #587ea8" class="">C</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">func</span> test() -> <span style="color: #587ea8" class="">B</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(133, 62, 100); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #587ea8" class="">fatalError</span><span style="color: #000000" class="">(</span>"just a stub"<span style="color: #000000" class="">)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div class=""><br class=""></div></div><div>Some languages support this, and Swift certainly could — though I don’t know that it’s a frequently request feature.</div><div><br class=""></div><div>It would also be interesting if associated type constraints allowed this, which I don’t think they currently do:</div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(50, 62, 125); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>protocol<span style="color: #000000" class=""> </span><span style="text-decoration: underline ; color: #000000" class="">C</span><span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">associatedtype</span> TestInput <span style="color: #323e7d" class="">where</span> <span style="color: #587ea8" class="">B</span>: <span style="color: #587ea8" class="">TestInput // error here</span></div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">func</span> test(x: <span style="color: #587ea8" class="">TestInput</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div class=""><br class=""></div></div><div>Curiously, the following does not work, although it seems like it should:</div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">protocol</span> A {}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">protocol</span> B: <span style="color: #587ea8" class="">A</span> {}</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(50, 62, 125); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>protocol<span style="color: #000000" class=""> C {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">associatedtype</span> TestOutput: <span style="color: #587ea8" class="">A</span></div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">func</span> test() -> <span style="color: #587ea8" class="">TestOutput</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">class</span> M: <span style="color: #587ea8" class="">C</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #323e7d" class="">func</span> test() -> <span style="color: #587ea8" class="">B</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(133, 62, 100); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span><span style="color: #587ea8" class="">fatalError</span><span style="color: #000000" class="">(</span>"just a stub"<span style="color: #000000" class="">)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div class=""><br class=""></div><div class="">It gives the error “inferred type 'B' (by matching requirement 'test()') is invalid: does not conform to ‘A’” even though B does conform to A. Huh.</div></div><div><br class=""></div><div>Cheers, P</div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 17, 2018, at 2:43 AM, Saagar Jha via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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; line-break: after-white-space;" class="">If we have:<div class=""><br class=""></div><div class="">class N: A {}</div><div class=""><br class=""></div><div class="">you can pass an N into C’s test(x:), since N is an A, but not M’s test(x:), since N is not a B. Thus, it’s not a valid conformance.</div><div class=""><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Saagar Jha</div>
</div>
<div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 17, 2018, at 00:04, Roshan via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi,<br class=""><br class="">Cross posting from swift-users in case this behaviour isn't part of<br class="">the language and might be interesting to you folks.<br class=""><br class="">Here is some sample code that gives a protocol conformance error in a<br class="">playground:<br class=""><br class="">protocol A {}<br class="">protocol B: A {}<br class=""><br class="">protocol C {<br class=""> func test(x: A)<br class="">}<br class=""><br class="">class M: C {<br class=""> func test(x: B) {}<br class="">}<br class=""><br class="">Is there a reason why the compiler doesn't infer that ((B) -> ())<br class="">matches ((A) -> ()) because of inheritance?<br class=""><br class="">-- <br class="">Warm regards<br class="">Roshan<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>