Support for returning the result of a Guard statement

Surprisingly neat! :+1:

I would be against this. "if" or another construct seems to cover these cases fine. We don't need to use guard everywhere we check for errors. They are just good for a certain class of error checking.

It looks like a new type of optional binding to me, but it is confusing because it has a side effect in the destructuring part of an expression. For example the following all seems pretty nonsense to me if it were extended to other contexts.

switch (1, 2) {
case (3, let n): print "something else: \(n)"
case (return _, 4): // can never get here because of the return...
default: 
}

Even this seems pretty buried and hard to follow.

guard let x = myOtherOptionalFunc(), return value = myOptionalFunc(x) else {
    throw SomeError
}

I do agree that I like Swift to find a good balance between terse and descriptive that I don't think it always finds. However, I think finding a shorter form probably makes more sense as an expression and Swift is still exploring that area. However, I think most of these will be simple cases of return or throw and just putting that in an expression as others have suggested might be the best way.

2 Likes