<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 &quot;let x = try foo() catch { ... }&quot;)? 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 &quot;let x; do { x = try foo() } catch { x = bar() }&quot;, or with &quot;let x = try? foo() ?? bar()&quot;. I just wonder if it&#39;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 &quot;catch&quot; block we have today.) But it&#39;s also nice to be able to glance at the code and see that, unambiguously, control flow can&#39;t escape the block.</div><div><br></div><div>The reason I originally suggested &quot;guard&quot; is readability: Seeing the word &quot;guard&quot;, a reader knows that control flow can&#39;t escape the else/catch block. But I understand why guard is still necessary for working with optionals/patterns, and I suppose seeing &quot;try&quot; 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&#39;t bind variables? Simply &quot;try foo() catch { ... }&quot; ?  (At one point I had considered &quot;<b>do</b> try ... catch ...&quot;, as a braceless analogue of &quot;<b>do</b> { try ... } catch ...&quot;.)</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">&lt;<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>&gt;</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 &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><div><blockquote type="cite"><b>I propose extending guard-statements to handle errors</b> without using the optional &quot;try?&quot; and without a do-block, by allowing the expression to throw, and offering &quot;catch&quot; instead of &quot;else&quot;:<br><div><div dir="ltr"><div><br></div><div>    // func foo() throws -&gt; T ...</div><div>    guard let x = <b>try</b> foo <b>catch</b> {</div><div>        print(&quot;the error was: \(<b>error</b>)&quot;)  // the &quot;error&quot; 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 &quot;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 -&gt; T ...</div></span><div>let x = try foo() catch {<span><br>        print(&quot;the error was: \(error)&quot;)  // the &quot;error&quot; 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 -&gt; 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(&quot;the error was: \(error)&quot;)  // the &quot;error&quot; 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&quot; 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>