[swift-evolution] Synthesizing Equatable, Hashable, and Comparable for tuple types
Alejandro Martinez
alexito4 at gmail.com
Mon Nov 27 10:25:36 CST 2017
>
> Ah. You seem to be unfamiliar with protocol existentials. Protocols
> (currently, only those without Self or associated type requirements, for
> various implementation reasons) are themselves types. For example, you can
> write:
>
A little offtopic, but I've been wanting to ask if it would be
possible to clarify the definition of existentials on the generics
manifesto (or somewhere than can be easily found by newcomers). I'm
sure I'm not the only one that has a vague notion of what it means but
would still appreciate to have a more concrete definition with some
examples. :D
> ```
> protocol P { }
> extension Int : P { }
> let x: P = 42
> ```
>
> In this example, x is of type `P`, not of type `Int`. Let's clarify the
> difference:
>
> ```
> extension Array where Element == P {
> func hi() {
> print("Hello")
> }
> }
>
> extension Array where Element : P {
> func hi() {
> print("World!")
> }
> }
>
> let y: [P] = [x]
> let z: [Int] = [x as Int]
>
> y.hi() // Prints "Hello"
> z.hi() // Prints "World!"
> ```
>
> Moreover, if we do not write the first `extension Array`, then `y.hi()`
> doesn't compile. This helps to illustrate that P does not conform to itself.
>
>
>>> For a type T : P, a tuple of type (T, T) is not a tuple of type (P, P).
>>> If we can extend tuples, you can write a generic algorithm that works with
>>> any type (T, T) where T : P, and/or you can write an algorithm that works
>>> with concrete type (P, P). Note that there is no overlap between these two
>>> because existential type P does not conform to protocol P.
>>>
>>
>> Mike
>>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
--
Alejandro Martinez
http://alejandromp.com
More information about the swift-evolution
mailing list