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

[SR-9934] Conditional binding (if let) does not work with try? expressions #52340

Closed
swift-ci opened this issue Feb 15, 2019 · 1 comment
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9934
Radar None
Original Reporter yasin.turkdogan (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

iOS 12.1.2, Swift 4.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 186af1cebd0fa81ac5b79ad8fd491033

Issue Description:

Optional binding ends up with nil value unexpectedly when the given function marked as throws and wrapped with try?

Sample code block :

func test() throws -> String?  {
    return nil
}

if let A = try? test() {
    print("Case 1. Value of A: \(String(describing: A))")
} else {
    print("Case 2")
}

//Output: Case 1. Value of A: nil
@belkadan
Copy link
Contributor

This is behaving as designed in Swift 4.2: the original return type is String?, i.e. Optional<String>, and so try? produces Optional<Optional<String>>. In Swift 5 mode this will change to have the flattening behavior you were expecting, per SE-0230.

Note that that change is limited to Swift 5 mode, for source compatibility reasons. That means updating your build settings if you want the new behavior.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants