Is `_openExistential` still a legit strategy for opening `Any.Type` for Swift 5.X?

Hi! Does anyone have any more insight into the status of _openExistential heading into Swift 6? Does anyone have more advice on opening existential metatypes when the existing language and compiler support from Swift 5.7 does not seem to compile?

I've been studying SE-0335[1] and SE-0352[2]. My specific use case is opening the Any.Type existential metatype. I have some code (working from Swift 5.9) that shows how I can use _openExistential to open a metatype at run-time and make the compiler happy:

struct S { }

func main() {
  func body<Value>(_: Value.Type) -> Value.Type { Value.self }
  
  let type = S.self
  let metatype: Any.Type = type

  let x = body(type)

  print(x)
  //  S

  let y = body(_openExistential(metatype, do: body))

  print(y)
  //  S

  let _ = body(metatype)
  //  Generic parameter 'Value' could not be inferred
  //  Cannot convert value of type 'any Any.Type' to expected argument type 'Value.Type'
}

main()

AFAIK the implicit opening from SE-0352 supports existential metatypes that conform to some arbitrary protocol… but what is not (yet) supported in Swift 5.X is to open existential metatypes that are Any.Type. Is this correct?

I've also found reports that Swift 6.0 will support code like body(metatype) directly (without the need for `_openExistential).[3][4] Is this correct?

If I want functionality like this today from Swift 5.X (open an existential metatype at runtime to pass to a generic function), do I have any legit options other than an underscored API like _openExistential? The SE-0352 proposal also says:

Weaken statements about fully subsuming _openExistential.

Could you please help me understand how code like this should be built from Swift 5.X? Thanks!


  1. swift-evolution/proposals/0335-existential-any.md at main · apple/swift-evolution · GitHub ↩︎

  2. swift-evolution/proposals/0352-implicit-open-existentials.md at main · apple/swift-evolution · GitHub ↩︎

  3. SE-0352: Implicitly Opened Existentials - #31 by Joe_Groff ↩︎

  4. Why is (any Any).Type different to any Any.Type? - #10 by Alejandro ↩︎

2 Likes

@Douglas_Gregor Did you have any more publicly available documentation or resources to follow along for any more information about that? Thanks!

In the Swift 6.0 compiler, there is a new ImplicitOpenExistentials feature flag to enable implicit opening of all existential types in the Swift 5 language mode.

This appears to be correct.

Until the Swift 6.0 compiler is officially released, it doesn’t seem so. SE-0352 says:

_openExistential might still have a few scattered use cases when opening an existential that doesn't have conformance requirements on it.

1 Like