[swift-evolution] Proposal: Allow explicit type parameter specification in generic function call
dabrahams at apple.com
Fri Dec 2 14:56:44 CST 2016
on Fri Dec 02 2016, Anton Zhilin <swift-evolution at swift.org> wrote:
> 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.
But the fact that generics are implemented with witness tables is almost
entirely an implementation detail that doesn't poke through into the
programming model. So it's hard to understand why you're stressing this
More information about the swift-evolution