[swift-evolution] try? shouldn't work on non-method-call

John McCall rjmccall at apple.com
Thu Aug 18 12:46:29 CDT 2016


> On Aug 18, 2016, at 10:15 AM, Davor Jankolija via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> This is a warning right now — do you think it should be an error?
>> 
>> Slavas-MacBook-Pro:~ slava$ cat ttt.swift 
>> func f() {}
>> 
>> func g() {
>> try f()
>> try? f()
>> }
>> 
>> Slavas-MacBook-Pro:~ slava$ swiftc ttt.swift 
>> ttt.swift:4:3: warning: no calls to throwing functions occur within 'try' expression
>> try f()
>> ^
>> ttt.swift:5:8: warning: no calls to throwing functions occur within 'try' expression
>> try? f()
>>      ^
> 
> IMHO at least, this should be an error. As a side note I do think that try should have greater precedence than as, even though this does potentially limit calls that would use only one try on a series of throwing statement that can be handled using parentheses. The benefit is that try is probably called in the vast majority of situations (that’s my experience at least) on a single throw statement and having it have higher precedance would avoid some surprises as most developers probably already assume that try wil be executed before as.

The design of ordinary 'try' and 'try!' are that they includes the entire expression to the right, spanning casts and other binary operators, and I think that's clearly the right rule for them.  It's not as clearly right for 'try?', but making 'try' and 'try?' be parsed differently would be horribly confusing.

John.


More information about the swift-evolution mailing list