<div dir="ltr"><div><div><div><div><div>Thank you for your responses. I'm grateful for having been given several different perspectives, especially in such a short time, and have some ideas for things to try this weekend.<br></div><br></div>If
I'm understanding correctly, the salient point is that Swift will
resolve a call to an overloaded function to exactly one implementation
of that overloaded function, even if for all types of a generic class'
generic function.<br></div><div><br></div>I'd like to understand Swift's
design philosophy better. Why were things implemented this way? For
example, in the following line of code:<br><br>let thisDoesntWork = Baz<UInt>().read()<br><br><br></div>The
compiler knows that read() is being called on Baz<UInt>, so it
could know that read() must return a UInt, so it could know that it can
safely use the Bar.read() that returns a UInt. From what I'm hearing,
this is what C++ would use, but not Swift, and I imagine there is a good
reason for it.<br><br></div><div>Wallacy, if you're able to provide a link to this WWDC talk I would greatly appreciate it.<br><br>Ryan<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 14, 2016 at 6:07 AM, Wallacy <span dir="ltr"><<a href="mailto:wallacyf@gmail.com" target="_blank">wallacyf@gmail.com</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">Generics are compile time feature, but like said, not like c++ templates.<div><div>Unconstrained generic T will be like Any. " return self.myBar.read()" can only be translated to a "function pointer" to the generic version, because is the only information available at compile time.</div><div>Has a talk on wwdc explaining this.</div><div>Swift can do generic specialization, but only when information enough to do this. Usually constrained generic is the way to go.</div></div><div><br></div><div><br><div class="gmail_quote"><div><div class="h5"><div dir="ltr">Em qui, 14 de jan de 2016 às 05:05, Ryan Conway via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> escreveu:<br></div></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><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>
</div></div><span class=""><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=fuWmDRE6-2FDdvdUHeLHruUfdigJaxz7nh99l6HfkulY9iTITLrUgXX84P32mHrSSWfDwmPWz84U4oWFa7K2o3Af-2BiOHczhG608i0qA08fDiTwcpWh30fQUmlQwUV2UqmJ4D0iTTMdee7jtJYSUylIO8t8tqXU5x7l76TqEt6xyAqo1SMlQc2-2BFkU0w6MrNyAepG9i3rIbVEZfNkRNWZ0bwTHpwnyVoRNzYmS4LTJ6Au0-3D" alt="" 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" border="0" height="1" width="1">
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">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>
</span></blockquote></div></div></div>
</blockquote></div><br></div>