<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Hi Ian,</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">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; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">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; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Alex</div> <br><p class="airmail_on">On June 9, 2016 at 16:05:47, Ian Terrell (<a href="mailto:ian.terrell@gmail.com">ian.terrell@gmail.com</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>


<title></title>


<div dir="ltr">Hi Alex,
<div><br></div>
<div>This is definitely a little confusing. I think it may be
intentional behavior though.</div>
<div><br></div>
<div>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><br></div>
<div>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><br></div>
<div>
<div>func printPrinter&lt;T: SignedIntegerType&gt;(printer:
PrintClass&lt;T&gt;) {<br></div>
<div>&nbsp; &nbsp; printer.printMe()</div>
<div>&nbsp; &nbsp; testPrint(printer.value)</div>
<div>}</div>
</div>
<div><br></div>
<div>Now the further constrained version of printPrinter is called,
which calls the further contrained version of printMe.</div>
<div><br></div>
<div>I hope this helps! And I hope if I got anything wrong someone
chimes in to correct me. :)</div>
<div><br></div>
<div>Ian</div>
<div><br></div>
<div><br></div>
<div><br></div>
<div><br></div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Wed, Jun 8, 2016 at 4:44 PM, Aleksandar
Petrovic via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi
swift-users,<br>
<br>
I'm trying achieve something similar to C++ template specialization
with protocol extensions, and I found a strange
behavior:&nbsp;<br>
<br>
// ----------<br>
<br>
protocol Printer {<br>
&nbsp; &nbsp; associatedtype TestType<br>
&nbsp; &nbsp; var value: TestType { get }<br>
&nbsp; &nbsp; func printMe()<br>
}<br>
<br>
extension Printer {<br>
&nbsp; &nbsp; func printMe() {<br>
&nbsp; &nbsp; &nbsp; &nbsp; print("Base printer: \(value)")<br>
&nbsp; &nbsp; }<br>
}<br>
<br>
extension Printer where TestType: SignedIntegerType {<br>
&nbsp; &nbsp; func printMe() {<br>
&nbsp; &nbsp; &nbsp; &nbsp; print("Int printer: \(value)")<br>
&nbsp; &nbsp; }<br>
}<br>
<br>
func testPrint&lt;T&gt;(value: T) {<br>
&nbsp; &nbsp; print("testPrint")<br>
}<br>
<br>
func testPrint&lt;T where T:SignedIntegerType&gt;(value: T) {<br>
&nbsp; &nbsp; print("testPrint for int")<br>
}<br>
<br>
<br>
class PrintClass&lt;T&gt;: Printer {<br>
&nbsp; &nbsp; var value: T<br>
&nbsp; &nbsp; init(value: T) { self.value = value }<br>
}<br>
<br>
func printPrinter&lt;T&gt;(printer: PrintClass&lt;T&gt;) {<br>
&nbsp; &nbsp; printer.printMe()<br>
&nbsp; &nbsp; testPrint(printer.value)<br>
}<br>
<br>
<br>
let intPrinter = PrintClass(value: 42)<br>
let stringPrinter = PrintClass(value: "test value")<br>
<br>
intPrinter.printMe() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;// Int printer: 42<br>
stringPrinter.printMe() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; // Base printer: test value<br>
<br>
testPrint(intPrinter.value) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//
testPrint for int<br>
testPrint(stringPrinter.value) &nbsp; &nbsp; // testPrint<br>
<br>
printPrinter(intPrinter) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; // Base printer: 42 &nbsp; &nbsp;(!!!)<br>
&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>
<br>
// ----------<br>
<br>
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>
<br>
Is this a bug or desired behaviour?<br>
<br>
Alex<br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote>
</div>
<br></div>


</div></div></span></blockquote></body></html>