<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 3, 2016, at 1:37 PM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On Jan 3, 2016, at 10:21 AM, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class=""><br class="">On Jan 3, 2016, at 12:12 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jan 2, 2016, at 2:23 PM, Tyler Cloutier &lt;<a href="mailto:cloutiertyler@aol.com" class="">cloutiertyler@aol.com</a>&gt; wrote:<br class=""><br class="">Please see comments inline.<br class=""><br class=""><blockquote type="cite" class="">On Dec 31, 2015, at 12:07 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Dec 27, 2015, at 10:25 PM, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">So “try” instead of “do”. If there is no catch, then just use braces without a keyword for a block.<span class="Apple-converted-space">&nbsp;</span><br class=""><br class="">And use do-while instead of repeat-while.<br class=""></blockquote></blockquote><br class="">+1<br class=""><br class=""><blockquote type="cite" class=""><br class="">Do you also propose no longer marking calls to throwing functions with `try`?<br class=""></blockquote><br class="">If try had both a single-statement/expression form as it does today, and a block form that makes it unnecessary to mark all the individual statements in the block, that would be an improvement.<br class=""><br class=""><blockquote type="cite" class="">Have you read the "Error-Handling Rationale" document in the Swift repository? If not, please do: &lt;<a href="https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst" class="">https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst</a>&gt; If so, please explain why you disagree with it.<br class=""></blockquote><br class="">There are large classes of programs where you can know you don’t care exactly where a failure happens, e.g. (most init functions, all pure functions, any function that doesn’t break invariants). &nbsp;In these cases marking every statement or expression that can throw is just noise. &nbsp;Try writing some serialization/deserialization code where the underlying stream can fail to see what I mean; you’ll have “try” everwhere, and it adds nothing to comprehensibility or maintainability. &nbsp;Personally I would like to be able to label the function itself and not have to introuce a scope, but IMO being able to create “try blocks” would be a welcome addition and would even match the common case in blocks with catch clauses, where being aware of the exact line where the error was generated is typically not useful.<br class=""></blockquote><br class="">I had proposed something very similar to this around six months ago on the swift-users list, but I think John McCall, had some (quite valid) concerns with this.<br class=""><br class="">Unfortunately I can't access those emails, but I think his concern was that the purpose of try was to mark explicitly which statements throw and this would defeat the purpose of that. People might just wrap large blocks in try.<br class=""></blockquote><br class="">As much as I am loath to disagree with John on this, there’s an incorrect implicit assumption in that rationale, that forcing people to mark all throw points trains them to get error-handling correct. &nbsp;What it does instead is to train them to think of all code uniformly instead of recognizing the places where a throw needs special attention (places where there are broken invariants). Eventually, as with warnings that have a high false-positive rate, when you see “try” in many places where it doesn’t help, you learn to ignore it altogether.<br class=""></blockquote><br class="">I agree that requiring this is not likely to result in improved error handling and thus is not a strong argument in favor of it.<br class=""><br class="">IMO the purpose of requiring “try” to be stated explicitly is that it arguably makes code more readable. &nbsp;It is immediately clear which functions can throw and which cannot. &nbsp;You don’t need to look up the signature of every function called to determine this. My experience thus far has been that I have really appreciated the requirement that throwing expressions be explicitly marked.<span class="Apple-converted-space">&nbsp;</span><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">As a default it’s great. &nbsp;Not having a way to opt out of individual marking for a whole block or function—because you know you’re not breaking any invariants, so which functions can throw is irrelevant, and not having a way for the compiler deduce these regions (e.g. known pure functions)—is the problem. &nbsp;The recognizer code posted in an earlier message is a perfect example. &nbsp;If there *was* some code where it was really important to notice failure points, you’d miss it.<span class="Apple-converted-space">&nbsp;</span></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>I feel like I must be missing something here. &nbsp;If we are able to mark the whole block with try I don’t see how we would notice any really important failure points. &nbsp;They would not be marked anywhere in the source:</div><div><br class=""></div><div>&nbsp; func recognizeHandler() throws {</div><div>&nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; accept(.on) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// .on is an enum tag for the token for the ‘on’ keyword.<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeName()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeFormalParamSeq()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(.newline)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeCommandSeq()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(.end)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeName() &nbsp;&nbsp;&nbsp;// Later Visitor pass checks that names match.&nbsp;<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(.newline)</div><div>&nbsp; &nbsp; }</div><div><div class=""><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote>&nbsp;&nbsp;}</div></div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The key to getting error handling right is not being able to trace every possible control path—which is effectively impossible anyway— it’s understanding the relationship between scopes in your code and your program’s invariants.<span class="Apple-converted-space">&nbsp;</span></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>I don’t disagree in terms of getting error handling right. &nbsp;</div><div><br class=""></div><div>My concern around this is not about getting error handling right or wrong, but rather being able to understand what is happening when I read a piece of code, especially code I am unfamiliar with. &nbsp;It is often very helpful to see where errors might be thrown. &nbsp;</div><div><br class=""></div><div>If we have a `try` block that removes the requirement to mark potentially throwing expressions without any further restrictions many developers will overuse that facility. &nbsp;It will almost surely be the de-facto default by force of habit and bad style in a lot of code. &nbsp;The advantages of the “great default” will be lost in this code. &nbsp;I think this is an important real-world concern to keep in mind.</div><div><br class=""></div><div>At the same time, I agree that in functions like recognizeHandler where all or nearly all statements / expressions can throw making the statements individually doesn’t tell us a lot. &nbsp;It does feel like maybe there should be a better way to handle this.</div><div><br class=""></div><div>What would you think about a solution that just inverted the default. &nbsp;Rather than marking throwing expressions with `try` we could have a try block (with optional catch clauses) where non-throwing calls are marked with `do`. &nbsp;The primary motivation for requiring `do` would be to prevent abuse of `try` blocks by making them awkward when there is a reasonable mix of throwing and non-throwing code. &nbsp;A secondary benefit is that would still be clear what can throw and what can’t, although this is much less useful when most things can throw.</div><div><br class=""></div><div>This would immediately tell a reader that a function like recognizeHandler can throw on every statement without cluttering up the individual statements. &nbsp;If there were a couple of non-throwing expressions it would still keep things cleaner than requiring `try` everywhere:</div><div><br class=""></div><div><div>&nbsp; func recognizeHandler() throws {</div><div>&nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; accept(do getTheOnTokenValue()) &nbsp; &nbsp; &nbsp; &nbsp;<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeName()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeFormalParamSeq()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(do getTheNewlineTokenValue())</div><div>&nbsp; &nbsp; &nbsp; &nbsp; recognizeCommandSeq()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(do getTheEndTokenValue())<br class="">&nbsp; &nbsp; &nbsp; &nbsp; recognizeName() &nbsp;<br class="">&nbsp; &nbsp; &nbsp; &nbsp; accept(do getTheNewlineTokenValue())</div><div>&nbsp; &nbsp; }</div><div><div class=""><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote>&nbsp;&nbsp;}</div><div class=""><br class=""></div><div class="">The applications of something like this might be pretty limited (because of the `do` requirement) but there are are cases like recognizeHandler that would benefit from it.</div><div class=""><br class=""></div><div class="">Obviously something like this this wouldn’t help code where a throwing and non-throwing code is mixed more evenly. &nbsp;Either way you would be required to mark quite a few expressions. &nbsp;I would have to see specific examples, but I think I would find most such code to be more easily understood with the annotations than without.</div></div></div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I think positions on both sides of this are reasonable.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Absolutely. &nbsp;Even reasonable positions can be sub-optimal though :-)</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>Agree. &nbsp;I think the problem here is that both positions on this are sub-optimal at different times. :)</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class=""><br class="">Another idea is to treat the block as an unnamed, no argument, no return value, function that could throw. This solves the problem in a very general way, and would retain the marking of all throwing functions with try,<br class=""></blockquote><br class="">That marking, in itself, is the root problem. &nbsp;Our syntax is the way it is primarily because "marking everywhere" was adopted as an explicit goal.<br class=""><br class=""><blockquote type="cite" class="">but has the perhaps unfortunate syntax of allowing things like:<br class=""><br class="">try {<br class="">try myFunction()<br class="">} catch {<br class=""><br class="">}<br class=""><br class="">Something like this could be shortened to a consistent theoretical inline try catch syntax like:<br class=""><br class="">try myFunction() catch {<br class=""><br class="">}<br class=""><br class="">Though, as John, pointed out at the time, this could still be added on with the current syntax. Obviously treating a try like an unnamed function would have different return semantics, so perhaps that's not the right abstraction. (Although I recall a thread going on that is considering allowing functions to retain return semantics of the outer scope)<br class=""><br class="">Tyler<br class=""><br class=""><br class=""><blockquote type="cite" class=""><br class="">-Dave<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote></blockquote><br class="">-Dave<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">-Dave</span></div></blockquote></div><br class=""></body></html>