<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<Int> and testPrint<String>, 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<T: SignedIntegerType>(printer:
PrintClass<T>) {<br></div>
<div> printer.printMe()</div>
<div> 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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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: <br>
<br>
// ----------<br>
<br>
protocol Printer {<br>
associatedtype TestType<br>
var value: TestType { get }<br>
func printMe()<br>
}<br>
<br>
extension Printer {<br>
func printMe() {<br>
print("Base printer: \(value)")<br>
}<br>
}<br>
<br>
extension Printer where TestType: SignedIntegerType {<br>
func printMe() {<br>
print("Int printer: \(value)")<br>
}<br>
}<br>
<br>
func testPrint<T>(value: T) {<br>
print("testPrint")<br>
}<br>
<br>
func testPrint<T where T:SignedIntegerType>(value: T) {<br>
print("testPrint for int")<br>
}<br>
<br>
<br>
class PrintClass<T>: Printer {<br>
var value: T<br>
init(value: T) { self.value = value }<br>
}<br>
<br>
func printPrinter<T>(printer: PrintClass<T>) {<br>
printer.printMe()<br>
testPrint(printer.value)<br>
}<br>
<br>
<br>
let intPrinter = PrintClass(value: 42)<br>
let stringPrinter = PrintClass(value: "test value")<br>
<br>
intPrinter.printMe()
// Int printer: 42<br>
stringPrinter.printMe()
// Base printer: test value<br>
<br>
testPrint(intPrinter.value) //
testPrint for int<br>
testPrint(stringPrinter.value) // testPrint<br>
<br>
printPrinter(intPrinter)
// Base printer: 42 (!!!)<br>
// testPrint
(!!!)<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(). <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>