Explicitly opening an existential type?

This feels like it might be an incredibly obvious question, but after I've watched and read through the advances in Swift 5.7 from WWDC, and in particular the implicitly opened existentials, I've been banging my head against the explicit side of that coin.

If you didn't want to use a function boundary to "implicitly open" an existential/"any" type, how is that possible today? Or is it only possible when you hand-code/wrap your own types?

In short, is there the equivalent concept of "explicitly opening an existential"?

1 Like

There’s a private standard library function called _openExistential, but you pass it a block to execute with the opened existential so even that operates at a function call boundary.

The closest thing to explicitly opening an existential is writing a local generic function:

func doSomethingWith(existential arg: any P) {
  func reallyDoSomethingWith<T: P>(openedExistential: T) {
    // real work happens here
  }
  reallyDoSomethingWith(openedExistential: arg) // implicitly opens existential argument
}
3 Likes