2016-12-02 3:55 GMT+03:00 Ramiro Feria Purón <ramiro.feria.pu...@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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to