<div dir="ltr">Thanks for the feedback, Chris.<div><br></div><div>To clarify, are you suggesting that plain variable bindings would support this too (such as "let x = try foo() catch { ... }")? I like this idea. I can also foresee the desire for something like this:</div><div><br></div><div> let x = try foo() catch {</div><div> print(error)</div><div> x = bar() // by some syntax, provide a fallback value for x, since foo() threw</div><div> // control flow *can* escape, since x has a value</div><div> }</div><div><br></div><div>Of course, you can achieve this today with "let x; do { x = try foo() } catch { x = bar() }", or with "let x = try? foo() ?? bar()". I just wonder if it's worth considering the possibility that this new feature would allow control flow to escape in some cases. (After all, control flow can exit the "catch" block we have today.) But it's also nice to be able to glance at the code and see that, unambiguously, control flow can't escape the block.</div><div><br></div><div>The reason I originally suggested "guard" is readability: Seeing the word "guard", a reader knows that control flow can't escape the else/catch block. But I understand why guard is still necessary for working with optionals/patterns, and I suppose seeing "try" introducing the expression may be enough.</div><div><br></div><div>Do you have thoughts on whether else/catch blocks should be re-orderable?</div><div><br></div><div>Another question: should it work with expressions that don't bind variables? Simply "try foo() catch { ... }" ? (At one point I had considered "<b>do</b> try ... catch ...", as a braceless analogue of "<b>do</b> { try ... } catch ...".)</div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Mon, Feb 29, 2016 at 10:34 PM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><span>On Feb 29, 2016, at 12:09 PM, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><div><blockquote type="cite"><b>I propose extending guard-statements to handle errors</b> without using the optional "try?" and without a do-block, by allowing the expression to throw, and offering "catch" instead of "else":<br><div><div dir="ltr"><div><br></div><div> // func foo() throws -> T ...</div><div> guard let x = <b>try</b> foo <b>catch</b> {</div><div> print("the error was: \(<b>error</b>)") // the "error" parameter is available here</div> // the compiler does not allow control flow to escape this block<div> }</div> // control flow cannot reach here if foo() threw an error</div></div></blockquote><br></div></span><div>I don’t think that this syntax makes sense, because you’re changing "guard let x = ” to not test an optional. The syntax you’re searching for seems more consistent as a modifier on var/let itself:</div><span><div><br></div><div>// func foo() throws -> T ...</div></span><div>let x = try foo() catch {<span><br> print("the error was: \(error)") // the "error" parameter is available here<br> // the compiler does not allow control flow to escape this block<br>}</span></div><div><br></div><div>The guard form of this would still make sense, but the existing “guard let” and “guard case” matching should work as it does. For example, something like this should be allowed:</div><div><br></div><div><div>// func bar() throws -> T?</div><div>guard let x = try bar() else {</div><div> // this runs if ‘bar’ returns nil.</div><span> // the compiler does not allow control flow to escape this block</span><span><div>} catch {<br> print("the error was: \(error)") // the "error" parameter is available here<br> // the compiler does not allow control flow to escape this block<br>}</div><div><br></div></span><div>More generally, the “guard” form would be workable on anything that takes a stmt-condition. This brings “if" and “while” into the mix.</div><span><font color="#888888"><div><br></div><div>-Chris</div></font></span></div></div></blockquote></div><br></div></div>