<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Hi Ryan,</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">I just try to explain this to you. I don't know Python. That is my background.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">I think in you code</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">class Baz<T> {</font></div><div class="gmail_default"><font face="georgia, serif"> let myBar = Bar()</font></div><div class="gmail_default"><font face="georgia, serif"> </font></div><div class="gmail_default"><font face="georgia, serif"> func read() -> T {</font></div><div class="gmail_default"><font face="georgia, serif"> return self.myBar.read()</font></div><div class="gmail_default"><font face="georgia, serif"> }</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><br></div><div class="gmail_default"><span style="font-family:georgia,serif">func read() -> T means you want the return type T method other than the other two.</span> Swift will not try the return type Int as it choose the function read() -> T before it replaces the type T to type Int. That is my option.</div><div class="gmail_default"><br></div><div class="gmail_default">Further more, if you change code as </div><div class="gmail_default"><br></div><div class="gmail_default"><div class="gmail_default">class Baz<T> {</div><div class="gmail_default"> let myBar = Bar()</div><div class="gmail_default"> </div><div class="gmail_default"> func read() -> T {</div><div class="gmail_default"> return self.myBar.read()</div><div class="gmail_default"> }</div><div class="gmail_default"> </div><div class="gmail_default"> func read() -> Int {</div><div class="gmail_default"> return self.myBar.read()</div><div class="gmail_default"> }</div><div class="gmail_default"> func read() -> UInt {</div><div class="gmail_default"> return 1</div><div class="gmail_default"> }</div><div class="gmail_default">}</div><div class="gmail_default"><br></div><div class="gmail_default">//let thisDoesntWork = Baz<UInt>().read() // ambitious code as swift does not know which function to choose</div><div class="gmail_default">let thisWillWork:Int = Baz<UInt>().read() // -1</div></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><div class="gmail_default">zhaoxin</div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 14, 2016 at 3:04 PM, Ryan Conway 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"><div dir="ltr"><span style="font-size:12.8px">Hey swift-users,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I'm teaching myself Swift, coming from a mostly C and Python background, and would like to understand generics more deeply. Right now, I'm seeing generic data types invoke overloaded methods in ways I do not understand, and am seeking clarification why.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">In an effort to model a data structure whose data can be represented as multiple data types simultaneously, I've made this class. Here its implementation is mocked using constants.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>class Bar {</div><div> func read() -> Int {</div><div> return -1</div><div> }</div><div> func read() -> UInt {</div><div> return 1</div><div> }</div><div> func read<T>() -> T {</div><div> print("Unsupported data type requested")</div><div> exit(1)</div><div> }</div><div>}</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Objects of that class return the requested type as expected when used like so:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>let thisWorks: Int = Bar().read() // returns -1</div><div>let thisAlsoWorks: UInt = Bar().read() // returns 1<br></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">However, when I introduce generics on top of that class, the expected method (the "most precise" method) is not called. For example, given this other class:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>class Baz<T> {</div><div> let myBar = Bar()</div><div> </div><div> func read() -> T {</div><div> return self.myBar.read()</div><div> }</div><div>}</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Both of these invocations call the generic read<T>() -> T method rather than the read() -> UInt method:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>let thisDoesntWork = Baz<UInt>().read()</div><div>let thisDoesntWorkEither: UInt = Baz<UInt>().read()</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Am I using generics wrong here? Is there some other language feature I should be using to capture this data? Any pointers would be greatly appreciated.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Thank you,</div><div style="font-size:12.8px">Ryan</div></div>
<img alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
<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>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><br>Owen Zhao<br></div></div></div>
</div>