<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><br class="">Begin forwarded message:<br class=""><font color="#5856d6" class=""><br class=""></font><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class=""><b class="">From: </b></span><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class="">Christopher Kornher <<a href="mailto:ckornher@me.com" class="">ckornher@me.com</a>></span><br class=""><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class=""><b class="">Subject: </b></span><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class=""><b class="">Re: [swift-evolution] [Pitch] Guard/Catch</b></span><br class=""><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class=""><b class="">Date: </b></span><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class="">July 10, 2017 at 5:10:15 PM MDT</span><br class=""><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class=""><b class="">To: </b></span><span style="font-family: -webkit-system-font, 'Helvetica Neue', Helvetica, sans-serif;" class="">Elviro Rocca <<a href="mailto:retired.hunter.djura@gmail.com" class="">retired.hunter.djura@gmail.com</a>></span><br class=""><font color="#5856d6" class=""><br class="">This messages was modified from the original</font> <font color="#5856d6" class="">accidentally </font><span style="color: rgb(88, 86, 214);" class="">sent out privately, earlier.</span></div><div><font color="#5856d6" class=""><br class=""></font>FYI this works today in Xcode 9.0 beta 2 playgrounds:<br class=""><font color="#5856d6" class=""><br class=""></font>```<br class=""><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">class</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> X {</span><br class=""><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> </span><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">init</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;">() </span><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">throws</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> {}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">func</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> foo() {</span><br class=""><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(62, 30, 129);">print</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">( </span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">"Things succeeded"</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> )</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> }</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">func</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> bar() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">throws</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> -> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> { </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">return</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() }</span><br class=""><font color="#d12f1b" class=""><br class=""><br class=""></font><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">func</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> f()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">{</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">guard</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">let</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x1:</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> = </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">? </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">(), </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">let</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x2:</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> = </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">? </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">bar</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">else</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> {</span><br class=""><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(62, 30, 129);">print</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">( </span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">"Things failed "</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">)</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">return</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> }</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x1.</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">foo</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x2.</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">foo</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">f</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(0, 132, 0);">// works</span><br class=""><span style="background-color: rgb(255, 255, 255);" class="">```</span><br class=""><font color="#ba2da2" face="Menlo" size="2" class=""><br class=""><br class=""></font>Most of the examples of this proposed feature don’t handle the exceptions other than to perform an early return. <br class="">So, without handing exceptions, the only unhandled case is a non-returning throwing function or init:<br class=""> <br class="">```<br class=""><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">class</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> X {</span><br class=""><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> </span><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">init</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;">() </span><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">throws</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> {}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">func</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> foo() {</span><br class=""><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(62, 30, 129);">print</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">( </span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">"Things succeeded"</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> )</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> }</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">func</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> bar() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">throws</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">{ </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">let</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">_</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> = </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() }</span><br class=""><font color="#d12f1b" class=""><br class=""><br class=""></font><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">func</span><span class="" style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 13px;"> f()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">{</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">do</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> {</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">bar</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> } </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">catch</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> {</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">return</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> }</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">guard</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">let</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x:</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> = </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">try</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">? </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(79, 129, 135);">X</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">else</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> {</span><br class=""><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;"> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(62, 30, 129);">print</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">( </span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">"Things failed "</span><span class="" style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px;">)</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(186, 45, 162);">return</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> }</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class=""> x.</span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">foo</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">()</span><br class=""><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">}</span><br class=""><font color="#d12f1b" class=""><br class=""></font><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(49, 89, 93);">f</span><span style="color: rgb(209, 47, 27); font-family: Menlo; font-size: 13px; background-color: rgb(255, 255, 255);" class="">() </span><span class="" style="font-family: Menlo; font-size: 13px; color: rgb(0, 132, 0);">// works</span><br class=""><span style="color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">```</span><br class=""><font color="#5856d6" class=""><br class=""></font>Having to call a throwing, Void method before performing the rest of a non-throwing function (or closure ) seems like an edge case to me. Perhaps I am just biased by my experience. I have not created or used many throwing initializers and certainly none in guard statements, and if they were in guard statements, the need to handle exceptions differently from any other guard failure seems ever more unlikely.</div><div><br class=""></div><div>I don’t think that the small rightward drift of exception handling is onerous. It is hardly like the “pyramid of doom” that ```guard``` was created to fix.</div><div><br class=""></div><div>```</div><div><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);" class="">func</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class=""> f( y:</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">? = </span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(186, 45, 162);" class="">nil</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class=""> )</span></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">do</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">try</span> <span style="color: #31595d" class="">bar</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">let</span> x:<span style="color: #4f8187" class="">X</span> = <span style="color: #ba2da2" class="">try</span> <span style="color: #4f8187" class="">X</span>()</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">guard</span> <span style="color: #ba2da2" class="">let</span> y = y <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3e1e81" class="">print</span>( <span style="color: #d12f1b" class="">"No y"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> x.<span style="color: #31595d" class="">foo</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3e1e81" class="">print</span>( <span style="color: #d12f1b" class="">"y=</span>\<span style="color: #d12f1b" class="">(</span>y<span style="color: #d12f1b" class="">)"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> } <span style="color: #ba2da2" class="">catch</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>// Handle some exceptions.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">return</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div class="">```</div></div><div><br class=""></div><div><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Helvetica; font-size: 12px;" class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""></div></div></div></div></div></div><div class="">On Jul 10, 2017, at 1:45 AM, Elviro Rocca via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote type="cite" class=""></blockquote><font color="#00afcd" class=""><br class=""></font>This is not a sugar proposal, in the same way as "guard" is not syntactic sugar, because it requires exiting the scope on the else branch, adding expressive power and safety to the call: also, the sugary part is pretty important because it avoids nested parentheses and very clearly states that if the guard condition is not fulfilled, the execution will not reach the next lines of code. Guard is useful to push the programmer to at least consider an early return instead of branching code paths, to achieve better clarity, readability and lower complexity, and I suspect is one of the best Swift features for many people.<br class=""><br class="">Also, the case that the proposal aims to cover is not an edge case at all for a lot of people, including me. Rethrowing an error is something that I almost never do, and I consider the "umbrella" do/catch at the top of the call stack an anti-pattern, but I understand that many people like it and I'm not arguing against it. I am arguing in favor of having options and not pushing a particular style onto programmers, and for my (and many people's) style, a guard/catch with forced return is an excellent idea. In fact you seem to agree on the necessity of some kind of forced-returnish catch but your elaborations don't seem (to me) much better than the proposal itself.<br class=""><br class="">Dave DeLong raised the point of weird behavior in the case of a function like:<br class=""><br class=""><br class="">func doSomething() throws → Result? { … }<br class=""><br class=""><br class="">In this case, what would the type of x be?<br class=""><br class=""><br class="">guard let x = try doSomething() catch { /// handle and return }<br class=""><br class=""><br class="">Simple, it would be Optional<Result>. I don't find this confusing at all, and if the idea that just by seeing "guard let" we should expect a non-Optional is somehow diffused, I think it's better to eradicate it.<br class=""><br class="">First of all, if I'm returning an optional from a throwing function, it's probably the case that I want the Optional to be there in the returned value: the only reason why I would consider doing that is if the semantics of Optional are pretty meaningful in that case. For example, when parsing a JSON in which I expect a String or null to be at a certain key:<br class=""><br class=""><br class="">extension String: Error {}<br class=""><br class="">func parseString(in dict: [String:Any], at key: String) throws -> String? {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard let x = dict[key] else { throw "No value found at '\(key)' in \(dict)" }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>if let x = x as? String { return x }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>if let x = x as? NSNull { return nil }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>throw "Value at '\(key)' in \(dict) is not 'string' or 'null"<br class="">}<br class=""><br class=""><br class="">Thus, if I'm returning an Optional from a throwing function it means that I want to clearly distinguish the two cases, so they shouldn't be collapsed in a single call:<br class=""><br class=""><br class="">guard let x = try doSomething() catch { /// handle and return }<br class="">guard let x = x else { /// handle and return }<br class=""><br class=""><br class="">Also, if a function returns something like "Int??", a guard-let (or if-let) on the returned value of that function will still bind an "Int?", thus unwrapping only "one level" of optional. If-let and guard-let, as of today, just unwrap a single optional level, an do not guaranteed at all that the bound value is not optional.<br class=""><br class="">To me guard-let (like if-let) is basically sugar for monadic binding for Optionals, with the additional expressivity granted by the forced return. I would love to see the same monadic binding structure applied to throwing functions.<br class=""><br class=""><br class=""><br class="">Elviro<br class=""><br class=""><br class=""><br class=""><font color="#00afcd" class=""><br class=""></font>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Il giorno 09 lug 2017, alle ore 01:16, Christopher Kornher via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> ha scritto:<br class=""><br class="">Thanks for you considerate reply. My concern over the proliferation of “sugar proposals” is a general one. This proposal has more merit and general utiliity than many others. I have never used a throwing function in a guard statement that was not itself in a throwing function, but I can see that it could possibly be common in some code. Wrapping a guard statement and all the code that uses variables set in the guard in a do/catch is sub-optimal.<br class=""><br class=""><br class="">All catches don’t have to exit the outer scope, so using guard only handles a subset <br class=""><br class="">It think that creating the terse try/catch for simple cases has multiple advantages:<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>1) I think that it addresses your desire for a simple way to use throwing functions easily in guard statements.<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>2) It avoids having to change the guard syntax to accomplish this<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>3) It is useful for handling simple one line try/catch constructs in less space in a way that should not seem too foreign to Swift developers.<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>4) It simplifies code that currently uses nested do/try/catch constructs. Even though this is rare, it introduces significant “rightward drift”.<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>5) It can used to return early from void throwing functions easily. e.g. : <br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>Multiple void throwing functions would probably be better handled by a do/catch block, but there is no danger of needing values from these functions because there are none:<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>I did not think of this before, but perhaps we could allow `do` to be replaced with `guard`, thereby allowing values to escape to the outer scope, while still ensuring an early exit:<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class="">I am not sure that “leaky” braces are a good idea, so perhaps some other character could be used to indicate a non-scope or whatever you want to call it:<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>```<br class="">This would make the language even harder to read, so just using braces is probably a better idea.<br class=""><br class="">This would change the guard syntax slightly, but is a straightforward extrapolation of do/catch and guard, I think. Of course, this could replace the existing guard syntax entirely and its use of semicolons, if we want to go that far…<br class=""><br class="">Allowing this syntax only if one of the expressions throws is possibly a good backward-compatible solution that would avoid redundant guard syntaxes.<br class=""><br class="">Anyway there are lot of possibilities here. We are not forced to extend the guard statement as it exists today. The current guard statement syntax was quite controversial when it was introduced and extending it may not be the best option to do what you want.<br class=""><br class="">- Chris<br class=""><br class=""><br class=""><br class=""><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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><blockquote type="cite" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><blockquote type="cite" class=""></blockquote>On Jul 8, 2017, at 4:16 PM, Benjamin Spratling via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><br class=""><br class="">I’ve read your email, but haven’t digested it fully. One thing I agree with is that most functions which call throwing functions don’t actually use a do…catch block, but instead are merely marked “throws” and the error is propagated back through the stack. Once I seriously started coding functions with errors, I realized I almost always wanted my errors to reach my view-controller or my business logic so I could present separate UI if a real error occurred, and often my error message depended on the details of the error instance.<br class=""><br class=""><br class=""><br class="">I disagree with your conclusion on this point.<br class="">The “guard” syntax is specifically designed to achieve early return (and placing code associated with early return at the point where it happens) and cleanly installing the returned value into the surrounding scope. So far it has been used to achieve early return only with optionals, true. But is that inherent to ‘guard’, or is it merely because that’s the only way it has been used? The guard does set variables that are needed in the body of the function, and that’s exactly why using guard with values returned from throwing functions makes so much sense, because it does exactly the same thing in a general sense. The “do”…”catch” structure is intentionally designed differently, to place the “happy path” in one place and place the returns in another place. I think with guard/else, we’re seeing developers who can handle less cognitive loading find it easier to reason about early return than grouping failures after the happy path. This proposal hopes to introduce that better language architecture to the catching of errors.<br class=""><blockquote type="cite" class=""><div class=""><div style="text-align: start; text-indent: 0px;" class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></blockquote>On Jul 8, 2017, at 4:08 PM, Christopher Kornher <<a href="mailto:ckornher@me.com" class="">ckornher@me.com</a>> wrote:<br class=""><br class="">I am opposed to this proposal because it muddies up the language to support what is essentially an edge case. The standard way to exit a function early because an exception is thrown is to make the function itself throw, if it is part of a larger operation. The addition of a few lines of try/catch code is not a great burden and makes the termination of an an exception very clear.<br class=""><div class=""><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></blockquote><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></blockquote>`guard` statements are generally used to set variables that are needed in the body of a function. Using them to save a few lines of exception handing code is a very different use. There is no need to mix two relatively clean syntaxes for a few edge cases and increase cognitive load one more time,<span class="Apple-converted-space"> </span><br class=""></div></div></div></div></blockquote><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>guard try foo( ) catch { return }</div></div></div><span class="Apple-tab-span" style="white-space: pre;">        </span>do {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn1()<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn2()<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>} catch {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// keep going, return or call a non-returning function, since throw is already handled by declaring a throwing enclosing function.<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// No varibles are needed by the outer block because none are set<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// So it is not clearly a guard-like statement<br class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<span class="Apple-tab-span" style="white-space: pre;">        </span> </div></div></div><span class="Apple-tab-span" style="white-space: pre;">        </span>guard {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn1()<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn2()<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>let x = fn3()<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>} catch {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// Must exit<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>} else {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// Must exit<br class=""><div class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<span class="Apple-tab-span" style="white-space: pre;">        </span> </div></div></div></div><span class="Apple-tab-span" style="white-space: pre;">        </span>guard <your favorite character here><br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn1()<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>try fn2()<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>let x = fn3()<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> <another favorite character here> catch {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// Must exit<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>} else {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>// Must exit<br class=""><div class=""><div class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<span class="Apple-tab-span" style="white-space: pre;">        </span> </div></div></div></div></div><br class="">-Ben Spratling<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=""><div class=""><div style="text-align: start; text-indent: 0px;" class=""><div class=""><div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></div></div></div></div></div></div></blockquote></div></div></div></blockquote></div></div></div></div><br class=""></body></html>