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

John McCall rjmccall at apple.com
Fri Oct 14 15:42:13 CDT 2016


> On Oct 11, 2016, at 12:04 AM, Karl via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> On 11 Oct 2016, at 08:49, Benjamin Spratling <bspratling at mac.com <mailto:bspratling at mac.com>> wrote:
>> 
>> 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
>> 
> 
> Yeah, I understand the reason for it, but I still think we should change it so you write the ‘try’ before the part which actually throws. Perhaps the rule should be something more general around rethrowing autoclosures?
> 
> After all, I thought that was the entire point of the ‘try’ keyword. The compiler doesn't really need it (it already knows what throws and what doesn’t), but it helps humans to mark where the throwing stuff happens.

I agree that you should be able to place the try inside the autoclosure and have that propagate out.  This doesn't need evolution discussion; please file a bug.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161014/03ae72eb/attachment.html>


More information about the swift-evolution mailing list