<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="">On Feb 29, 2016, at 12:09 PM, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><div><blockquote type="cite" class=""><b class="">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 class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class=""> // func foo() throws -> T ...</div><div class=""> guard let x = <b class="">try</b> foo <b class="">catch</b> {</div><div class=""> print("the error was: \(<b class="">error</b>)") // the "error" parameter is available here</div> // the compiler does not allow control flow to escape this block<div class=""> }</div> // control flow cannot reach here if foo() threw an error</div></div></blockquote><br class=""></div><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><div><br class=""></div><div>// func foo() throws -> T ...</div><div>let x = try foo() catch {<br class=""> print("the error was: \(error)") // the "error" parameter is available here<br class=""> // the compiler does not allow control flow to escape this block<br class="">}</div><div><br class=""></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 class=""></div><div><div>// func bar() throws -> T?</div><div>guard let x = try bar() else {</div><div> // this runs if ‘bar’ returns nil.</div> // the compiler does not allow control flow to escape this block<div>} catch {<br class=""> print("the error was: \(error)") // the "error" parameter is available here<br class=""> // the compiler does not allow control flow to escape this block<br class="">}</div><div class=""><br class=""></div><div class="">More generally, the “guard” form would be workable on anything that takes a stmt-condition. This brings “if" and “while” into the mix.</div><div class=""><br class=""></div><div class="">-Chris</div></div></body></html>