<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div class=""><span style="background-color: rgba(255, 255, 255, 0);">class M: C {</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);"> func test(x: A) {}</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div>there's no "override" keyword, so I think you're overloading. </div><br><div id="AppleMailSignature"><div>C. Keith Ray</div><div><a href="https://leanpub.com/wepntk" style="background-color: rgba(255, 255, 255, 0); font-size: 13pt;">https://leanpub.com/wepntk</a><span style="background-color: rgba(255, 255, 255, 0); font-size: 13pt;"> <- buy my book?</span></div><div><a href="http://agilesolutionspace.blogspot.com/">http://agilesolutionspace.blogspot.com/</a></div><div>twitter: @ckeithray</div><div><a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a></div></div><div><br>On Jan 17, 2018, at 11:53 AM, Saagar Jha via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html; charset=utf-8">Interestingly, it seems like Swift allows for contravariance for subclassing, in that this is valid code:<div class=""><br class=""></div><div class=""><div class="">protocol A {}</div><div class="">protocol B: A {}</div><div class=""><br class=""></div><div class="">class C { // Notice this is a class, not a protocol</div><div class=""> func test(x: B) {}</div><div class="">}</div><div class=""><br class=""></div><div class="">class M: C {</div><div class=""> func test(x: A) {}</div><div class="">}</div><div class=""><br class=""></div><div class="">Is it an oversight that this doesn’t apply to protocols?</div><div class=""><br class=""></div><div class="">
<div style="color: rgb(0, 0, 0); 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; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Saagar Jha</div>
</div>
<div><br class=""><blockquote type="cite" class=""><div class="">On Jan 17, 2018, at 11:38, Paul Cantrell <<a href="mailto:cantrell@pobox.com" class="">cantrell@pobox.com</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=""><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="" class=""> </span>protocol<span style="" 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 class=""><br class=""></div><div class=""><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="" class=""> </span>protocol<span style="" 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="" class=""> </span><span style="color: #587ea8" class="">fatalError</span><span style="" class="">(</span>"just a stub"<span style="" 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 class="">Some languages support this, and Swift certainly could — though I don’t know that it’s a frequently request feature.</div><div class=""><br class=""></div><div class="">It would also be interesting if associated type constraints allowed this, which I don’t think they currently do:</div><div class=""><br class=""></div><div class=""><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="" class=""> </span>protocol<span style="" class=""> </span><span style="text-decoration: underline;" class="">C</span><span style="" 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 class="">Curiously, the following does not work, although it seems like it should:</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=""> <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="" class=""> </span>protocol<span style="" 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="" class=""> </span><span style="color: #587ea8" class="">fatalError</span><span style="" class="">(</span>"just a stub"<span style="" 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 class=""><br class=""></div><div class="">Cheers, P</div><div class=""><br class=""></div><div class=""><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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>