# Implicit existential opening doesn't seem to capture the type

**URL:** <https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676>\
**Category:** Using Swift\
**Tags:** generics, type-casting, existential\
**Created:** [March 13, 2023, 8:22pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676 "2023-03-13T20:22:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitri](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmitri](https://forums.swift.org/u/dmitri)\
**Post date:** [March 13, 2023, 8:22pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/1 "2023-03-13T20:22:49Z")

</div>

Hi all,  
I was expecting that the results of calling `_openExistential` and using implicit existential opening would be the same, but when I run the following:

```swift
func genericFunction<T>(_ v: T) {
    print("value: \(v)", "typeUsingTself: \(T.self) typeUsingTypeOf: \(type(of: v))" )
}
func doSomeWork(x: Any) {
    _openExistential(x, do: genericFunction)
    genericFunction(x)
}
doSomeWork(x: 0)
doSomeWork(x: 0.0)
doSomeWork(x: "zero")

```

I get:

```swift
value: 0 typeUsingTself: Int typeUsingTypeOf: Int
value: 0 typeUsingTself: Any typeUsingTypeOf: Any
value: 0.0 typeUsingTself: Double typeUsingTypeOf: Double
value: 0.0 typeUsingTself: Any typeUsingTypeOf: Any
value: zero typeUsingTself: String typeUsingTypeOf: String
value: zero typeUsingTself: Any typeUsingTypeOf: Any

```

Does anyone know why simply calling `genericFunction(x)` won't capture the type inside the function?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 13, 2023, 8:30pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/2 "2023-03-13T20:30:05Z")

</div>

Your generic function has no constraints, so it won’t open any existentials. I think this was done to avoid changing existing code that already passed Any into unconstrained generic positions.

---

<div class="post-metadata">

**Author:** ![dmitri](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmitri](https://forums.swift.org/u/dmitri)\
**Post date:** [March 13, 2023, 8:34pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/3 "2023-03-13T20:34:41Z")

</div>

Is there anything else I could do to capture the type of Any and use the type in a generic context, besides using `_openExistential` function?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 13, 2023, 8:45pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/4 "2023-03-13T20:45:30Z")

</div>

I don’t believe so _as such,_ but what do you want to do with it that you can’t do with `type(of: blah)`?

---

<div class="post-metadata">

**Author:** ![dmitri](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmitri](https://forums.swift.org/u/dmitri)\
**Post date:** [March 13, 2023, 8:58pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/5 "2023-03-13T20:58:49Z")

</div>

I want to have the following:

```swift
protocol ProtocolA {
     associatedtype Value
}
struct StructA<Value>: ProtocolA {
     var value: Value
     // ...

}

```

and

```swift
func makeA(_ value: Any) -> any ProtocolA {
   // create an instance of StructA using an appropriate type 
   // (not just Any) and return it as any ProtocolA 
}

```

I can easily do that when `Value` is constrained by some protocol, but what if I wanted Value to be anything, without having to invent a special protocol to make this work?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 13, 2023, 9:05pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/6 "2023-03-13T21:05:29Z")

</div>

I think you’ve found the one case that isn’t covered by the existing rules: wrapping a value in some kind of container. I don’t know of a workaround; maybe somebody more recently working on Swift does.

---

<div class="post-metadata">

**Author:** ![dmitri](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmitri](https://forums.swift.org/u/dmitri)\
**Post date:** [March 13, 2023, 9:07pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/7 "2023-03-13T21:07:21Z")

</div>

This certainly works

```swift
protocol ProtocolA {
    associatedtype Value
}
struct StructA<Value>: ProtocolA {
    var value: Value
}
func genericFunction<T>(_ v: T) -> any ProtocolA {
    StructA<T>(value: v)
}
func makeA(x: Any) -> any ProtocolA {
    _openExistential(x, do: genericFunction)
}
print(makeA(x: 0))
print(makeA(x: 0.0))
print(makeA(x: "zero"))

```

but my worry is that some platforms may not allow this due to the `_openExistential` usage.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 13, 2023, 9:23pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/8 "2023-03-13T21:23:16Z")

</div>

> [@dmitri](#):
>
> Does anyone know why simply calling `genericFunction(x)` won't capture the type inside the function?

It doesn’t have anything to do with “capturing”—the static type really is `Any`, and that’s because `genericFunction`’s generic parameter has no constraints, so `Any` is just fine without having to open the existential to pass the argument to the function.

For backward compatibility with versions of Swift before implicitly opened existentials were implemented, this behavior is preserved for Swift 5 even in releases where the feature is now implemented. For the Swift 6 language mode, the existential will be opened as you’re expecting.

See the original proposal text for more explanation [here](https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md#avoid-opening-when-the-existential-type-satisfies-requirements-in-swift-5).

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [March 13, 2023, 9:23pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/9 "2023-03-13T21:23:18Z")

</div>

Why do you need to pass `Any`, instead of …?

```swift
func makeA(x: some Any) -> any ProtocolA {
  genericFunction(x)
}
// or
func makeA<T>(x: T) -> any ProtocolA {
  genericFunction(x)
}

```

---

<div class="post-metadata">

**Author:** ![dmitri](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmitri](https://forums.swift.org/u/dmitri)\
**Post date:** [March 13, 2023, 9:55pm UTC](https://forums.swift.org/t/implicit-existential-opening-doesnt-seem-to-capture-the-type/63676/10 "2023-03-13T21:55:31Z")

</div>

Because in my use cases, I want to be able to pass the result of  
`if let value = someStruct[keyPath: keyPath]` where `keyPath` is stored as `AnyKeyPath`.

So

```swift
protocol ProtocolA {
    associatedtype Value
}
struct StructA<Value>: ProtocolA {
    var value: Value
}
func genericFunction<T>(_ v: T) -> any ProtocolA {
    StructA<T>(value: v)
}
func makeA(x: some Any) -> any ProtocolA {
    genericFunction(x)
}
struct AnotherStruct {
    var value: Double
}
let anotherStruct = AnotherStruct(value: 1.0)
let kp: AnyKeyPath = \AnotherStruct.value

if let result = anotherStruct[keyPath: kp] {
    print(makeA(x: result))
}

```

gives me `StructA<Any>(value: 1.0)`, whereas

```swift
protocol ProtocolA {
    associatedtype Value
}
struct StructA<Value>: ProtocolA {
    var value: Value
}
func genericFunction<T>(_ v: T) -> any ProtocolA {
    StructA<T>(value: v)
}
func makeA(x: Any) -> any ProtocolA {
    _openExistential(x, do: genericFunction)
}
struct AnotherStruct {
    var value: Double
}
let anotherStruct = AnotherStruct(value: 1.0)
let kp: AnyKeyPath = \AnotherStruct.value
if let result = anotherStruct[keyPath: kp] {
    print(makeA(x: result))
}

```

produces correct output `StructA<Double>(value: 1.0)`.
