[swift-evolution] Proposal: Allow explicit type parameter specification in generic function call
Anton Zhilin
antonyzhilin at gmail.com
Fri Dec 2 05:34:11 CST 2016
2016-12-02 3:55 GMT+03:00 Ramiro Feria Purón <ramiro.feria.puron at gmail.com>:
> *Unlike C++'s templates, a Swift's generic function is semantically a
> single function.*
>
> Anton, could you provide further insight on this?
>
Basically, generic functions in Swift are implemented as functions taking
an additional parameter -- witness table, which contains implementation of
all functions in the protocol.
For example, the following:
func f<T: P>(a: T, b: T)
Turns into something like:
func f(a: Any, b: Any, witnessTableForT: UnsafePointer<_WitnessTable>)
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't view them equally, and
current situation, where we can "explicitly specialize" types, but not
functions, is not an inconsistency.
I used word "semantically", because compiler can apply *specialization
optimization* to a function, which is usually followed by inlining. But
this optimization is guaranteed not to break the above assumptions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161202/0a2bc320/attachment.html>
More information about the swift-evolution
mailing list