[swift-evolution] Enhanced Existentials

Braeden Profile jhaezhyr12 at gmail.com
Tue Jan 10 19:57:20 CST 2017


Looking at that proposal on Austin’s Github, would its complete implementation require/allow for any type that’s known at runtime to be used like a type at compile time?  So that these two blocks would both work?  It seems like the runtime would be using the same technology, but it’s hard to tell.
let a : Collection where .Element == Int = // ...
let b : Collection where .Element == Int = // ...

func someGenericFunc<C : Collection>(x: C, y: C) where C.Element == Int {
    // ...
}

if let openedA = a as? a.Self, let openedB = b as? a.Self {
    // openedA is type a.Self; openedB is type a.Self
    // We now know that openedA and openedB are the same concrete type, which
    // conforms to Collection with Elements that are Ints
    // This is okay
    someGenericFunc(x: openedA, y: openedB)
}
let a : Collection where .Element == Int = // ...
let b : Collection where .Element == Int = // ...

func someGenericFunc<C : Collection>(x: C, y: C) where C.Element == Int {
    // ...
}

let desiredType = type(of: a)
if let openedA = a as? desiredType, let openedB = b as? desiredType {
    // openedA is type a.Self; openedB is type a.Self
    // We now know that openedA and openedB are the same concrete type, which
    // conforms to Collection with Elements that are Ints
    // This is okay
    someGenericFunc(x: openedA, y: openedB)
}

assert(type(of: a) === a.Self.self)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170110/96a3356a/attachment.html>


More information about the swift-evolution mailing list