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?
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.
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.