[swift-evolution] Idea: Extend "guard" to try-statements, with a catch block

Stephen Celis stephen.celis at gmail.com
Mon Feb 29 15:03:01 CST 2016


> On Feb 29, 2016, at 3:09 PM, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Thoughts?

Thanks for writing this up! I think it's a natural step to take from the current model.

> Here are some discussion topics:
> 
> - If Swift's error-handling mechanisms evolved into a first-class Result type, would this proposal be moot?

Could you clarify what you mean by this?

> - Would this make sense as a feature of pattern-matching, rather than just "guard", so you could also do "if case let x = try foo() { ... } catch { ... }" ?

This makes sense to me at first glance, though it makes things rather complicated.

  - Can `catch` blocks appear before `else [if]` blocks? E.g.,

        if try test() != nil {
            ...
        } catch {
            ...
        } else if try test2() != nil {
            ...
        } else {
            ...
        } catch {
            ...
        }

  - Are `if`/`else` blocks generally promoted to `do`-like state when `catch` blocks exist?

        if something() {
            try failable()
        } catch {
            ...
        }

  - Does this extend to `switch` and how?

--
Stephen


More information about the swift-evolution mailing list