Howdy,
The error message is not saying that aFunction throws, it says “??" might 
throw.  After all, you supplied a ()rethrows->(Int) to it as its second 
argument, which is wrapping a ()throws->Int, “bFunction()"
?? and && and || wrap the trailing expression in an @autoclosure.

I am a little surprised two “try” are not required.  This would be my 
expectation:
> let value = try aFunction() ?? try bFunction()
but, using try to the right of a non-assignment operator is not allowed.

This, however, is not disallowed:

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

The purpose of the @autoclosure is to make developers forget they need to write 
a closure, and it apparently worked for you.

-Ben Spratling


> On Oct 11, 2016, at 1:16 AM, Karl via swift-evolution 
> <[email protected]> wrote:
> 
> You might expect this 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)
> 
> Instead, you must put the ‘try’ before the entire expression:
> 
> let value = try aFunction() ?? bFunction()
> 
> This is awkward, since aFunction() doesn’t throw.
> I propose we change the grammar to allow the first example and disallow the 
> second, consistent with the idea that throwing calls are ‘marked’ by using 
> the try keyword.
> 
> Karl
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to