<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-12-02 3:55 GMT+03:00 Ramiro Feria Purón <span dir="ltr">&lt;<a href="mailto:ramiro.feria.puron@gmail.com" target="_blank">ramiro.feria.puron@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class="gmail-"><div><span style="font-size:13px"><i>Unlike C++&#39;s templates, a Swift&#39;s generic function is semantically a single function.</i></span><br></div><div><br></div></span>Anton, could you provide further insight on this?</div></blockquote><div><br></div><div>Basically, generic functions in Swift are implemented as functions taking an additional parameter -- witness table, which contains implementation of all functions in the protocol.</div><div>For example, the following:</div><div><br></div><div>func f&lt;T: P&gt;(a: T, b: T)</div><div><br></div><div>Turns into something like:</div><div><br></div><div>func f(a: Any, b: Any, witnessTableForT: UnsafePointer&lt;_WitnessTable&gt;)</div><div><br></div><div>Returning to my cited phrase, Swift specializes generic types (with duplicated metadata; think C++ class specialization), but implements generic functions without using specialization. One can say that a generic type in Swift becomes a collection of distinct types, but a generic function remains a single function. So we can&#39;t view them equally, and current situation, where we can &quot;explicitly specialize&quot; types, but not functions, is not an inconsistency.</div><div><br></div><div>I used word &quot;semantically&quot;, because compiler can apply <i>specialization optimization</i> to a function, which is usually followed by inlining. But this optimization is guaranteed not to break the above assumptions.</div></div></div></div>