Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Created August 17, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoSoto/19499b0a52513c1671c9 to your computer and use it in GitHub Desktop.
Save NachoSoto/19499b0a52513c1671c9 to your computer and use it in GitHub Desktop.
// Compile with `swiftc switch.swift`
enum A {
case A(Int)
case B(Bool)
case C
case D
}
enum B {
case A
case B
}
func s(a: A, b: B) {
// error: switch must be exhaustive, consider adding a default clause
// WTF
switch (a, b) {
case let (.A(i), .A):
break
case let (.A(i), .B):
break
case let (.B(boolean), b):
break
case (.C, _), (.D, _):
break
}
}
@esttorhe
Copy link

case let (.B(boolean), b):
            break

the second b isn't supposed to be .B?

Also aren't you missing .B(boolean), .A ?

@Cananito
Copy link

Looks like it can’t infer b’s cases when a is either .C or .D, only this works:

func s(a: A, b: B) {
    switch (a, b) {
    case let (.A(i), .A):
        print(i)
        break
    case let (.A(i), .B):
        print(i)
        break
    case let (.B(boolean), .A):
        print(boolean)
        break
    case let (.B(boolean), .B):
        print(boolean)
        break
    case (.C, B.A):
        break
    case (.C, B.B):
        break
    case (.D, B.A):
        break
    case (.D, B.B):
        break
    }
}

Note: this was done in Swift 2.0 beta 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment