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-11415] Try? with nil-coalescing operator does not resolve #53816

Open
idrougge opened this issue Sep 4, 2019 · 1 comment
Open

[SR-11415] Try? with nil-coalescing operator does not resolve #53816

idrougge opened this issue Sep 4, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation

Comments

@idrougge
Copy link

idrougge commented Sep 4, 2019

Previous ID SR-11415
Radar None
Original Reporter @idrougge
Type Bug
Environment

Xcode 10.3 (10G8), Swift 5

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

md5: de8ec3fb2fc24920ffff647e79d3f055

Issue Description:

Given a throwing function, e.g.:

    func toss() throws -> Int {
        return 123
    }

Calling it using try? and handling the resulting optional using the ?? operator will not resolve to a non-optional:

let n: Int = try? toss() ?? 999

…instead giving the error message

Value of optional type 'Int?' not unwrapped; did you mean to use 'try!' or chain with '?'?

…as well as a fixit to replace try? with try!.

In Xcode, the function name will also not be highlighted as a function name until you apply the fixit, turning "try?" into "try!".

Wrapping the call in parentheses, e.g.:

let n: Int = (try? toss()) ?? 999

…the code will highlight and compile, but the expression should be solvable without.

@belkadan
Copy link
Contributor

belkadan commented Sep 4, 2019

This is behaving as expected, with try? having a lower precedence than ?? to be consistent with try. I admit it's not so useful in this case, though.

Any change here would have to go through the Swift Evolution Process.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

2 participants