# Pitch: 'Case Expressions' for Pattern-matching

**URL:** <https://forums.swift.org/t/pitch-case-expressions-for-pattern-matching/20348>\
**Category:** Pitches\
**Created:** [February 11, 2019, 5:41pm UTC](https://forums.swift.org/t/pitch-case-expressions-for-pattern-matching/20348 "2019-02-11T17:41:11Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 12, 2019, 6:04pm UTC](https://forums.swift.org/t/pitch-case-expressions-for-pattern-matching/20348/8 "2019-02-12T18:04:58Z")

</div>

> [@pommy](#):
>
> `case .someEnumCase(_, someMatchingValue) = x` is in fact an assignment (to `someMatchingValue` ) . It also does return a boolean value to the `if` -statement, similar to `if var v = x {` .

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

```swift
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:

> [@Unwrap or Throw - Make the Safe Choice Easier](https://forums.swift.org/t/unwrap-or-throw-make-the-safe-choice-easier/14453):
>
> The !! unwrap or die discussion, pointed out there is another way to handle optionals. Right now we have no language support to throw if the item is nil. this would be preferable than only having !! for the following reasons (although would not reduce the usefulness of that when you don't want to make your method throwable): A single line solution, rather than guard let or if let or if value != nil Makes it easier to do the right thing, that is, write safe code, by handling the nil rather t…

> [@Control Flow Expressions](https://forums.swift.org/t/control-flow-expressions/90):
>
> Hello. It would be cool if control flow statements like "switch", "if" can be used as expressions, such as in languages like Scala, Rust and Kotlin, so instead of writing: var message: String if x % 2 == 0 { message = "x is even" } else { message = "x is odd" } you can write: let message: String = if x % 2 == 0 { "x is even" } else { "x is odd" } I'm not into any kind of compiler development, and don't know is it feasible to introduce such change at the moment, but at least, I would l…

---

_[View the full topic](https://forums.swift.org/t/pitch-case-expressions-for-pattern-matching/20348)._
