Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SE-0352] Amendment to open existential arguments for optional parameters #1797

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Sep 27, 2022

SE-0352 "Implicitly Opened Existentials" has a limitation that prevents the opening of an existential argument when the corresponding parameter is optional. This amendment proposes to change that behavior, so that such a call will succeed when a (non-optional) existential argument is passed to a parameter of optional type:

func acceptOptional<T: P>(_ x: T?) { }
func test(p: any P, pOpt: (any P)?) {
  acceptOptional(p) // currently does not open "p", this amendment will open "p" and bind "T" to its underlying type
  acceptOptiona(pOpt) // does not open "pOpt", because there is no "T" to bind to when "pOpt" is "nil"
}

The rationale for not opening the existential p in the first call was to ensure consistent behavior with the second call, in an effort to avoid confusion. However, experience with implicitly-opened existentials has shown that opening an existential argument in the first case is important, because many functions accept optional parameters. Additionally, the workaround requires a lot of boilerplate, writing a separate generic function.

There is an implementation of this amendment at swiftlang/swift#61321

@rjmccall
Copy link
Contributor

SE-0352 has been released, right? I think this needs to be run as a new proposal.

@DougGregor
Copy link
Member Author

SE-0352 has been released, right? I think this needs to be run as a new proposal.

Yes, SE-0352 was in Swift 5.7. I'll turn this into a standalone proposal.

@DougGregor DougGregor closed this Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants