[swift-evolution] [Pitch] Change location of 'try' for infix operators

Benjamin Spratling bspratling at mac.com
Tue Oct 11 01:49:30 CDT 2016


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 <swift-evolution at swift.org> 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
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161011/1c70b7a1/attachment.html>


More information about the swift-evolution mailing list