[swift-evolution] [Pitch] Guarding on enum values

Kevin Ballard kevin at sb.org
Sun Dec 27 19:26:39 CST 2015


On Wed, Dec 23, 2015, at 03:35 PM, Joe Groff via swift-evolution wrote:
> A slight generalization would be to allow for an arbitrary pattern in the `else` clause:
> 
> guard case let .Succeed(m) = returnsResult() else case let .Failure(r) {
>        return r
> }
> 
> with the requirement that the "guard" and "else" patterns form an exhaustive match when taken together. That feels nicer than special-case knowledge of two-case enums, though I admit it punishes what's likely to be a common case.

Cute, but it seems a little confusing. Personally, I'd rather just stick with a normal switch in cases like that:

let m: MyType
switch returnsResult() {
case let .Succeed(m_): m = m_
case let .Failure(r): return r
}

-Kevin


More information about the swift-evolution mailing list