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-2963] Awkward position of 'try' for rethrowing autoclosures #45553

Open
karwa opened this issue Oct 15, 2016 · 0 comments
Open

[SR-2963] Awkward position of 'try' for rethrowing autoclosures #45553

karwa opened this issue Oct 15, 2016 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@karwa
Copy link
Contributor

karwa commented Oct 15, 2016

Previous ID SR-2963
Radar None
Original Reporter @karwa
Type Bug
Environment

Swift 3.0

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

md5: a92d394202d798ec2b328dc8aa2d0161

Issue Description:

You might expect the following code to work:

func aFunction() -> Int?       { return 5 }
func bFunction() throws -> Int { return 4 }

let value = aFunction() ?? try bFunction() // ERROR: Operator can throw but expression is not marked with a ‘try'
print(value)

The workaround is to place 'try' before the entire expression:

let value = try aFunction() ?? bFunction()

This is awkward, because `aFunction` isn't throwable; `bFunction` is. We ought to be able to place the `try` before `bFunction()` and have that propagate out.

The '??' operator is implemented with a rethrowing autoclosure:

public func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T)
        rethrows -> T { ... }

The behaviour would likely make sense for all functions which rethrow from autoclosures.

@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
Projects
None yet
Development

No branches or pull requests

1 participant