<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">+1 for the idea of extending guard to allow catches (which still have to exit scope) before the else statement:</div><div class=""><br class=""></div><div class="">guard let x = try foo() catch MyError.errorType {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//Handle error & exit scope</div><div class="">} catch MyError.otherErrorType {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//Handle error & exit scope</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//x was nil</div><div class="">}</div><div class=""><br class=""></div><div class="">I think that this will come in handy when we start dealing with Results, and as Haravikk mentioned, it lets us use the resulting variables in the surrounding scope (They aren't trapped in the do block).</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">><i class=""> On 16 Nov 2016, at 11:06, Nick Keets via swift-evolution <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">swift-evolution at swift.org</a>> wrote:
</i>><i class="">
</i>><i class=""> Hello all, I'm interested in a pattern to return failure, together with an error
</i>><i class=""> to explain why. I think that the "guard let x = x" discussion touched on the issue,
</i>><i class=""> but didn't really go in that direction.
</i>><i class="">
</i>><i class=""> Right now, optional and boolean results work nicely with guards, but you only get
</i>><i class=""> sucess/failure back. For example:
</i>><i class="">
</i>><i class=""> func foo() -> Int?
</i>><i class=""> func bar() -> Bool
</i>><i class=""> func baz(Int) -> Int?
</i>><i class="">
</i>><i class=""> guard
</i>><i class="">         let a = foo(),
</i>><i class="">         bar(),
</i>><i class="">         let b = baz(a)
</i>><i class=""> else {
</i>><i class="">         // No information about what failed here
</i>><i class="">         print("Something failed")
</i>><i class="">         return
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> I see a lot of enum Result solutions being proposed, but they have the fundamental
</i>><i class=""> problem of not having access to the error inside guards. For example:
</i>><i class="">
</i>><i class=""> enum Result<T> { case sucess(T), error(String) }
</i>><i class="">
</i>><i class=""> func foo() -> Result<Int>
</i>><i class="">
</i>><i class=""> guard case let .success(value) = foo() else {
</i>><i class=""> // Result is .error but we have no access to the error message here
</i>><i class=""> return
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> I think a solution to this problem could be to allow "guard let try" statements
</i>><i class=""> that make the error available inside the else statement. So you could write:
</i>><i class="">
</i>><i class=""> func foo() throws -> Int
</i>><i class=""> func bar() -> Bool
</i>><i class=""> func baz(Int) throws -> Int
</i>><i class="">
</i>><i class=""> guard
</i>><i class="">         let a = try foo(),
</i>><i class="">         bar(),
</i>><i class="">         let b = try baz(a)
</i>><i class=""> else {
</i>><i class="">         // `error` is available here like in a catch block
</i>><i class="">         print("Error: \(error.localizedDescription)")
</i>><i class="">         return
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> A potential weirdness of this solution is that it appears indistinguishable from
</i>><i class=""> "guard let try?" (already available) if you are not interested in the error.
</i>><i class="">
</i>><i class=""> Thoughts?
</i>
I think it comes to a point where you may be expecting too much of guard, when this is really what a switch or try/catch is for.
One of the purposes of switch is exhaustive handling for enums, so that's the best way to handle them if you're interested in more than one case. Whereas try/catch is exactly what you want if you want to, well, catch an error.
However, I wouldn't mind if maybe we could just have an optional catch on guard statements, like so:
        guard let a = try foo(), baz(), let b = try baz(a)
                else { return; } // baz() returned false, or foo() or baz() returned nil
                catch(error) { print("Error: \(error.localizedDescription)"); return } // foo() or baz() threw an exception
This would still have the same requirements as a guard's else statement (must change flow with break, return, throw etc.) but is only triggered in the exception case. If your guard condition can *only* fail as a result of an exception then you can omit the else. For example, if you skip the call to bar(), and foo() and baz(a) can't return nil then the following would be valid:
        guard let a = try foo(), let b = baz(a) catch(error) { print("Error: \(error.localizedDescription)"); return }
The main benefit here being that you get the same kind of in-scope a and b variables, unlike a try/catch block where they are limited to their own scope only (or you have to declare them outside which is messy).
I don't see how the enum case could be usefully simplified though, as you'd need to have pattern matching of some kind to define the error condition you expect, in which case you might as well just use a switch anyway, whereas a guard/catch at least eliminates an annoyance of try/catch when dealing with something that only fails initially.</pre></blockquote><div class=""><br class=""></div></div></body></html>