[swift-evolution] [Manifesto] Completing Generics

Developer devteam.codafi at gmail.com
Wed Mar 2 21:57:09 CST 2016

> Existentials
> Opening existentials
> Generalized existentials as described above will still have trouble with protocol requirements that involve Self or associated types in function parameters. For example, let’s try to use Equatable as an existential:
> protocol Equatable {
>   func ==(lhs: Self, rhs: Self) -> Bool
>   func !=(lhs: Self, rhs: Self) -> Bool
> }
> let e1: Equatable = …
> let e2: Equatable = …
> if e1 == e2 { … } // error: e1 and e2 don’t necessarily have the same dynamic type
> One explicit way to allow such operations in a type-safe manner is to introduce an “open existential” operation of some sort, which extracts and gives a name to the dynamic type stored inside an existential. For example:
> if let storedInE1 = e1 openas T {     // T is a the type of storedInE1, a copy of the value stored in e1
>   if let storedInE2 = e2 as? T {      // is e2 also a T?
>     if storedInE1 == storedInE2 { … } // okay: storedInT1 and storedInE2 are both of type T, which we know is Equatable
>   }
> }

Isn't "open existential" code for "casting ∃ to ∀"? Dispatch on the underlying type is brittle and anti-modular.  I should know, I tried to recover GADTs under the old system!  I shudder to think of what further horrors I could concoct with this pattern formalized in the language.

~ Robert Widmann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160302/57ad145c/attachment.html>

More information about the swift-evolution mailing list