<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Having foo available within the catch would be very strange as it is not yet in scope.</div><div><br></div><div>-Thorsten </div><div><br>Am 07.02.2016 um 11:00 schrieb Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><style>body{font-family:Helvetica,Arial;font-size:13px}</style>@Félix as far as I know you won’t be able to fall through be accident:<div><br></div><div><div><b>func throwingFuncReturns() throws -> Int? {</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>return 42</b></div><div><b>}</b></div><div><b><br></b></div><div><b>func use(foo: Int) {</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>print(foo)</b></div><div><b>}</b></div><div><b><br></b></div><div><b>func scope() {</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>let foo: Int? // saving will work as long as the type is an optional too</b></div><div><b><br></b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>do { foo = try throwingFuncReturns() } catch { </b></div><div><b><br></b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>// INITIALIZE foo OR RETURN <-- copiler will be happy</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>guard let unwrappedFoo = foo else { </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>return</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>use(unwrappedFoo)</b></div><div><b>}</b></div><div><b><br></b></div><div><b>scope() // to be able to return</b></div><div><br></div><div>This was added in Swift 1.2 for `if else` as far as I know.</div><div><br></div><div><b>let x: SomeThing</b></div><div><b>if condition {</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>x = foo()</b></div><div><b>} else {</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>x = bar()</b></div><div><b>}</b></div><div><b>use(x)</b></div><div><br></div><div>So porting this behavior to a single `<b>do try catch</b>` will work as it did before:</div><div><br></div><div><b>func scope() {</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>let foo: Int? // saving will work as long as the type is an optional too</b></div><div><b><br></b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>do foo = try throwingFuncReturns() catch { </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>// INITIALIZE foo OR RETURN <-- copiler will be happy</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>guard let unwrappedFoo = foo else { </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>return</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>use(unwrappedFoo)</b></div><div><b>}</b></div><div><br></div><div>Simplifying everything with the `<b>guard</b>` syntax might help us with optionals, but it automatically will close the door for `<b>fallthrough</b>`, because the original mechanism does not work this way. This will just confuse everyone.</div><div><br></div><div>It might sound absurd, but what if we remove the `<b>do</b>` keyword from the single `<b>do try catch</b>` and leave it as `<b>try catch</b>` with the some new behavior:</div><div><br></div><div>f<b>unc scope() {</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>let foo: Int? = try throwingFuncReturns() catch { </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>// foo IS AVAILABLE INSIDE THE CATCH BODY</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>// INITIALIZE foo OR RETURN</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>guard let unwrappedFoo = foo else { </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>return</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>use(unwrappedFoo)</b></div><div><b>}</b></div><div><br></div><div>The `<b>do</b>` body is needed if we want to execute more than one operation from its body, but it isn’t needed for a single `<b>try catch</b>` mechanism at all. So why would we want to keep the keyword here? We can omit the type from the throwing function that returns a value if no errors occurred. Plus we could apply an extra rule and assign the value from the catch body then fall through or return/break.</div></div><div><br></div><div>This also won’t have any impact on existing codebase.</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>