(As I prepared and almost finished writing up the question, re-reading the appropriate language guide section answered it for me, but possibly the Q&A can be of use for others, so I'll post it nonetheless)
Background
Consider the following enum
, with cases that have one of two different types of associated values, Int
or String
:
enum Foo {
case bar(Int)
case baz(Int)
case bax(Int)
case fox(String)
}
When performing pattern matching in a switch
statement, we may construct compound cases, each covering several possible matching patterns (entering the case
branch if any of the patterns match):
func foo(_ foo: Foo) -> Int {
switch foo {
case .bar, .baz, .bax: return 42
case .fox: return 0
}
}
Just like non-compound cases, compound cases may also include value binding:
func foo(_ foo: Foo) -> Int {
switch foo {
case .bar(let x), .baz(let x), .bax(let x): return x
case .fox(let y): return Int(y) ?? 0
}
}
// or
func foo(_ foo: Foo) -> Int {
switch foo {
case let .bar(x), let .baz(x), let .bax(x): return x
case let .fox(y): return Int(y) ?? 0
}
}
Question
- Is it possible to use a single common value binding for compound cases, which covers the compound of several
enum
cases that have the same type of associated value?
E.g., in the latter value binding examples above, some way to use a single binding functionality for the common-type associated value in the compound case
// not valid
func foo(_ foo: Foo) -> Int {
switch foo {
case .bar, .baz, .bax, (let x): return x
case .fox: return 0
}
}