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

Karl razielim at gmail.com
Tue Oct 11 01:16:06 CDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161011/26052f89/attachment.html>


More information about the swift-evolution mailing list