Enhanced Existentials

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)

A post was merged into an existing topic: Enhanced Existentials

Terms of Service

Privacy Policy

Cookie Policy