<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=""><div class="">Swift generics are not like C++ templates; they are built on dynamic dispatch at runtime instead of instantiation, much like Java interfaces. Overload resolution, on the other hand, happens at compile-time, with "more specific” overloads being preferred over “less specific” ones. (The exact ordering rules have not been written down anywhere.) So the behavior you’re seeing is by design: if you want static dispatch you can use overloads, and if you want dynamic dispatch you use a protocol.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 9, 2016, at 07:23, <a href="mailto:apetrovic@outlook.com" class="">apetrovic@outlook.com</a> via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">Hi Ian,</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">You're right, if there's a function that's specialized everything works well (actually, I deleted exactly the same function from my example, in an attempt to be brief and succinct :) ). But that kind of ruins the whole point of interface specialization and violates the principle of least surprise, don't you think?</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">I don't have enough knowledge about how Swift compiler implements generics to speculate is the "right" solution possible or not. If the compiler emits different implementations for functions testPrint&lt;Int&gt; and testPrint&lt;String&gt;, then it definitely have enought info to pick the right specialized implementation, and this is a bug; If not... well, then it's probably best to remove the whole feature from the language.</div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class=""><br class=""></div><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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; margin: 0px;" class="">Alex</div><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><p class="airmail_on" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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;">On June 9, 2016 at 16:05:47, Ian Terrell (<a href="mailto:ian.terrell@gmail.com" class="">ian.terrell@gmail.com</a>) wrote:</p><blockquote type="cite" class="clean_bq" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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;"><span class=""><div class=""><div class=""></div><div class=""><div dir="ltr" class="">Hi Alex,<div class=""><br class=""></div><div class="">This is definitely a little confusing. I think it may be intentional behavior though.</div><div class=""><br class=""></div><div class="">I don't believe it's tied to scope, but tied to the fact (I think!) that all generic specialization methods are statically dispatched.</div><div class=""><br class=""></div><div class="">Although it looks like the printMe method chosen would be based on the T of the specialized class at runtime, it's actually based on the T of the printPrinter method at compile time. At that time printPrinter has no information about T, and so it is tied to the general version of printMe. You can see that if you add a specialized printPrinter method:</div><div class=""><br class=""></div><div class=""><div class="">func printPrinter&lt;T: SignedIntegerType&gt;(printer: PrintClass&lt;T&gt;) {<br class=""></div><div class="">&nbsp; &nbsp; printer.printMe()</div><div class="">&nbsp; &nbsp; testPrint(printer.value)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">Now the further constrained version of printPrinter is called, which calls the further contrained version of printMe.</div><div class=""><br class=""></div><div class="">I hope this helps! And I hope if I got anything wrong someone chimes in to correct me. :)</div><div class=""><br class=""></div><div class="">Ian</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Jun 8, 2016 at 4:44 PM, Aleksandar Petrovic via swift-users<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">Hi swift-users,<br class=""><br class="">I'm trying achieve something similar to C++ template specialization with protocol extensions, and I found a strange behavior:&nbsp;<br class=""><br class="">// ----------<br class=""><br class="">protocol Printer {<br class="">&nbsp; &nbsp; associatedtype TestType<br class="">&nbsp; &nbsp; var value: TestType { get }<br class="">&nbsp; &nbsp; func printMe()<br class="">}<br class=""><br class="">extension Printer {<br class="">&nbsp; &nbsp; func printMe() {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; print("Base printer: \(value)")<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">extension Printer where TestType: SignedIntegerType {<br class="">&nbsp; &nbsp; func printMe() {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; print("Int printer: \(value)")<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">func testPrint&lt;T&gt;(value: T) {<br class="">&nbsp; &nbsp; print("testPrint")<br class="">}<br class=""><br class="">func testPrint&lt;T where T:SignedIntegerType&gt;(value: T) {<br class="">&nbsp; &nbsp; print("testPrint for int")<br class="">}<br class=""><br class=""><br class="">class PrintClass&lt;T&gt;: Printer {<br class="">&nbsp; &nbsp; var value: T<br class="">&nbsp; &nbsp; init(value: T) { self.value = value }<br class="">}<br class=""><br class="">func printPrinter&lt;T&gt;(printer: PrintClass&lt;T&gt;) {<br class="">&nbsp; &nbsp; printer.printMe()<br class="">&nbsp; &nbsp; testPrint(printer.value)<br class="">}<br class=""><br class=""><br class="">let intPrinter = PrintClass(value: 42)<br class="">let stringPrinter = PrintClass(value: "test value")<br class=""><br class="">intPrinter.printMe() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Int printer: 42<br class="">stringPrinter.printMe() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Base printer: test value<br class=""><br class="">testPrint(intPrinter.value) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// testPrint for int<br class="">testPrint(stringPrinter.value) &nbsp; &nbsp; // testPrint<br class=""><br class="">printPrinter(intPrinter) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Base printer: 42 &nbsp; &nbsp;(!!!)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// testPrint &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(!!!)<br class=""><br class="">// ----------<br class=""><br class="">The compiler correctly chooses specialized protocol extension as long as the function call is in the same scope with the object declaration. But all knowledge about types seems to be lost in the last line, when the scope is changed, in function printPrinter().&nbsp;<br class=""><br class="">Is this a bug or desired behaviour?<br class=""><br class="">Alex<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></blockquote></div><br class=""></div></div></div></span></blockquote><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">_______________________________________________</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">swift-users mailing list</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><a href="mailto:swift-users@swift.org" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">swift-users@swift.org</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: 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="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br class=""></body></html>