<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 dir="ltr">Em qui, 14 de jan de 2016 às 05:05, Ryan Conway via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> escreveu:<br></div><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 src="https://u2002410.ct.sendgrid.net/wf/open?upn=fuWmDRE6-2FDdvdUHeLHruUfdigJaxz7nh99l6HfkulY9iTITLrUgXX84P32mHrSSWfDwmPWz84U4oWFa7K2o3Af-2BiOHczhG608i0qA08fDiTwcpWh30fQUmlQwUV2UqmJ4D0iTTMdee7jtJYSUylIO8t8tqXU5x7l76TqEt6xyAqo1SMlQc2-2BFkU0w6MrNyAepG9i3rIbVEZfNkRNWZ0bwTHpwnyVoRNzYmS4LTJ6Au0-3D" 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>
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>
</blockquote></div></div></div>