Pitch: 'Case Expressions' for Pattern-matching

It only "assigns" to someMatchingValue (really, "binds"), if you write case .someEnumCase(_, let someMatchingValue). Otherwise it's part of the pattern:

enum SomeEnum { case oneCase; case anotherCase(String, Int) }
let x: SomeEnum = ...

if case .anotherCase(_, 42) = x { /* ... */ }

if case is definitely a weird construct, and you've mentioned some of the really awkward limitations it has. I think it would be worth making a general effort to make most of our language constructs usable as expressions. For example, the return and assignment examples could be handled by making if an expression.

The topic has come up several times before and it appears that some members of the core team support the idea. You might want to also check out these threads:

1 Like