[swift-evolution] [Manifesto] Completing Generics
Slava Pestov
spestov at apple.com
Wed Mar 2 22:50:05 CST 2016
> On Mar 2, 2016, at 7:57 PM, Developer via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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.
Think of it more as “casting P to a new generic type parameter T : P”, where P is a protocol type. Your code will not have static knowledge of the concrete type bound to T — you just know it’s some type that conforms to P.
It’s interesting that protocol extensions today get you some of the way there — the existential is effectively opened to a special generic type parameter “Self" inside the body of a protocol extension method:
protocol P {
…
}
func doSomething<T : P>(t: T) {
… do stuff with T
}
extension P {
func doSomething() {
doSomething(self)
}
}
let p: P = …
p.doSomething()
Slava
>
> ~ Robert Widmann
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list