# Proposal sanity check: assigning a case statement to a boolean

**URL:** <https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584>\
**Category:** Discussion\
**Created:** [September 24, 2020, 4:09pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584 "2020-09-24T16:09:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [September 24, 2020, 4:09pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/1 "2020-09-24T16:09:27Z")

</div>

Given that `case` can appear in an `if` statement without variable assignment, it has me scratch my head a bit why it can't be used to assign a boolean value.

In other words, seeing as this code works,

```swift
enum Enum {
  case aCase
  case anotherCase
}

let value = Enum.aCase

if case .aCase = value {
  print("a case")
} else {
  print("another case")
}

```

then it follows that this should as well,

```swift
let bool = (case .acase = value)

```

The only stumbling point seems to be that it uses a single equals sign instead of a double equals sign, which doesn't make much sense in the first place. In the `if case` if there's no variable assignment the syntax should also use a double equals to be clear, perhaps even if there is variable assignment.

It could be that the original proposal did not consider all of the ramifications it would have. This could leave room open for new proposal to tackle these cases, as it were.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [September 24, 2020, 4:42pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/2 "2020-09-24T16:42:18Z")

</div>

I think most of `case` [patterns](https://docs.swift.org/swift-book/ReferenceManual/Patterns.html) don't make sense to be boolean. The only real case would be on complex `enum` without value bindings. Even then, it does overlap a lot with normal `==` operator.

---

<div class="post-metadata">

**Author:** ![David\_Catmull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_catmull/32/1007_2.png) [@David\_Catmull](https://forums.swift.org/u/David_Catmull)\
**Post date:** [September 24, 2020, 4:57pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/3 "2020-09-24T16:57:51Z")

</div>

Why would you want to write it that way rather than `let bool = value == .aCase`?

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [September 24, 2020, 5:05pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/4 "2020-09-24T17:05:12Z")

</div>

It's not a "case expression" in an `if` statement, it's an `if case` statement, in the same way that `if let` is not an `if` statement with a "let expression".

Also, it really is an assignment, although you have to look at a more general form:

```swift
if case .aCase(let associatedValue) = enumValue

```

There's a generalized/conceptual source and destination here, and a value is being copied from the source to the destination, which is what an assignment does.

I agree it's a bit weird when there's no associated value. It's just one of those Swift quirks that you have to memorize.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [September 24, 2020, 5:43pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/5 "2020-09-24T17:43:35Z")

</div>

This won't work either when the enum has a payload, which I presume is what we are trying to solve here. I agree with Ilias that it is often extremely useful to "make any enum case equatable" so to speak.

I think others have pitched ways to compare just the bare cases of enum case with payloads, and this seems like the most natural way.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 24, 2020, 5:50pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/6 "2020-09-24T17:50:06Z")

</div>

Being able to assign the result of pattern matching to a boolean would be great. The main issue is that the pattern wouldn't be able to bind any variables, since there would be no scope for those variables to live in, but being able to get the result of the test is useful.

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [September 24, 2020, 6:45pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/7 "2020-09-24T18:45:07Z")

</div>

There's certainly a nice little feature that can potentially be implemented here, which would allow ergonomic checks even when the value is not Equatable (e.g. a payload is not Equatable) or you want to check for specific literal payloads. I've filed [[SR-13601] Boolean-valued case checking expressions · Issue #56036 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-13601) for a slightly more general version of what you've brought up in the example.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 24, 2020, 6:55pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/8 "2020-09-24T18:55:39Z")

</div>

More syntax bikeshedding: `case color is .rgb(1.0, _, _)`.

Upside: not confusable with an operator or an assignment.  
Downside: order is reversed from today's `if case`.  
Upside: today's `if case` is also considered weird, and it's bad for code completion when you type left-to-right, and this would fix that too.  
Downside: has nothing to do with the other `is`, which is even part of pattern syntax already (`case is UIView:`).

```swift
let flag = case enumValue is .aCase
if case enumValue is .aCase(let associatedValue) { … }

```

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [September 24, 2020, 7:24pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/9 "2020-09-24T19:24:18Z")

</div>

How about `color is case .rgb`, since `case` usually appears with the specified enum case?

---

<div class="post-metadata">

**Author:** ![xAlien95](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xalien95/32/12558_2.png) [@xAlien95](https://forums.swift.org/u/xAlien95)\
**Post date:** [September 24, 2020, 7:26pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/10 "2020-09-24T19:26:03Z")

</div>

> [@jrose](#):
>
> More syntax bikeshedding: `case color is .rgb(1.0, _, _)` .
> 
> Downside: has nothing to do with the other `is` , which is even part of pattern syntax already ( `case is UIView:` ).

I would personally prefer `is case` as a standalone token as much as `for case` or `if case` do.  
It's also easier to read and cannot be mistaken for `is` (`is` accepts a type, `is case` would only accept a case, which is either an Enum value or an Enum constructor function, which are values and not types):

```swift
let flag = enumValue is case .aCase
if enumValue is case .aCase { ... }

```

Edit: @GreatApe beated me with the timing 😂

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 24, 2020, 7:30pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/11 "2020-09-24T19:30:13Z")

</div>

That absolutely reads better as an expression :-) I'm a little worried about it doing funny things in `if` though:

```swift
if case .aCase(let assoc) = enumValue { // today
if enumValue is case .aCase(let assoc) { // okay
if (enumValue is case .aCase(let assoc)) == false { // error!
if enumValue is case .aCase(let assoc) == false { // um

```

I mean, we can make that work (basically by delaying the "can you put bindings here" check to after the "what kind of `if` is this" check), but it feels subtle to me in a way that the connected initial `if case` doesn't. Maybe that's just me, though.

For what it's worth, `is case` would _not_ only accept a `case`; it would accept any pattern, the same as switches and ifs:

```swift
let isOnXAxis = point is case (_, 0)

```

I don't think this is a problem, though!

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [September 24, 2020, 7:33pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/12 "2020-09-24T19:33:24Z")

</div>

But would you need to be able to bind when using this new construct? Can't you always revert to `if case` when there is binding going on? To me the second example is a bit confusing, and at least this time it _is_ unnecessary, right?

And why is the third one not ok?

---

<div class="post-metadata">

**Author:** ![xAlien95](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xalien95/32/12558_2.png) [@xAlien95](https://forums.swift.org/u/xAlien95)\
**Post date:** [September 24, 2020, 7:35pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/13 "2020-09-24T19:35:45Z")

</div>

> [@jrose](#):
>
> `if case enumValue is .aCase(let associatedValue) { … }`

I would not use `is case` to extract associated values since the `let/var` keywords shouldn't be on the right side in an assignment expression. I would use something like `as? case` or `as! case` to extract payloads as written [here](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/256) and let enumerations gain the same benefits optionals have.

```swift
let associatedValue = enumValue as! case .aCase // force unwrapping
if let associatedValue = enumValue as? case .aCase { ... } // unwrapping

```

But this is far beyond the purpose of this thread.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 24, 2020, 7:39pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/14 "2020-09-24T19:39:46Z")

</div>

Oh huh, I didn't even think of extending this to the `as?` / `as!` spectrum. I'll avoid nitpicking your examples because you're right that it's out of scope. My main thoughts about having this new syntax work with `if` were mostly about having related constructs look alike; it's too bad that `switch` and `is case` would look pretty similar and `if case` would be the odd one out. But you're right, we don't need to support that for this other part to be a good idea.

> [@GreatApe](#):
>
> And why is the third one not ok?

```swift
if (enumValue is case .aCase(let assoc)) == false { // error!

```

In this example, `assoc` is only valid to use if the case matches, but "if the case matches" doesn't say anything about "the body of the `if` will run" if it's composed with arbitrary expressions, so the compiler can't assume in general that `assoc` is safe to use. That's (part of) what Joe meant by "there would be no scope for those variables to live in".

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [September 24, 2020, 7:41pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/15 "2020-09-24T19:41:06Z")

</div>

I've always considered adding expression syntax for pattern-matching to be bound up with the idea of supporting something like regular expressions. And I wouldn't want the syntax to only work with a Bool result; you ought to be able to extract matched values in some way. So it's actually a rather large feature that needs to be thought through, not something we should add just because we can easily imagine its simplest form.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [September 24, 2020, 7:44pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/16 "2020-09-24T19:44:20Z")

</div>

Ah ok. But how about a solution where `is case` is used merely to make enum cases with associated payloads "equatable" without supplying or binding a case? In other words, it would only be used as an expression:

```swift
enum G {
    case one(Int)
    case two(Int, Int)
}
...
let isOne = someValue is case .one

```

This is the usage I personally miss the most. My intuition says that if we ever want to bind the associated value, then we can instead use `if case` as today, but I may be wrong...

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [September 24, 2020, 7:45pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/17 "2020-09-24T19:45:21Z")

</div>

Excuse my lack of imagination, but what else could be the result of the pattern matching, except a Bool?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 24, 2020, 7:51pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/18 "2020-09-24T19:51:17Z")

</div>

> [@John\_McCall](#):
>
> I've always considered adding expression syntax for pattern-matching to be bound up with the idea of supporting something like regular expressions. And I wouldn't want the syntax to only work with a Bool result; you ought to be able to extract matched values in some way. So it's actually a rather large feature that needs to be thought through, not something we should add just because we can easily imagine its simplest form.

I've thought about this too, and the conclusion I've come to is that something that produces bindings is _always_ going to be attached to a scope. For anything that _doesn't_ produce bindings, a method call or operator is fine. But we don't have that for structural patterns because they're part of the language rather than the library—we don't have a representation of a "pattern" that can be passed around at runtime.

The other thought I've had is that something like regex captures, which do both matching _and_ binding, are _extremely_ difficult to make syntax for that's both (reasonably) easy to read _and_ statically type-safe. I absolutely want us to solve that problem but I don't want to block other things on it.

_(This_ idea has long been blocked on the idea that we'd generate "implicit properties" on enums for checking and destructuring a particular case, but even that doesn't cover other sorts of pattern matching, so maybe we shouldn't let that block this idea either.)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [September 24, 2020, 7:57pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/19 "2020-09-24T19:57:27Z")

</div>

> [@jrose](#):
>
> I've thought about this too, and the conclusion I've come to is that something that produces bindings is _always_ going to be attached to a scope.

Well, if it produces a _binding_, yes, the bound name has to have a scope. But it could also produce a _result_, which we could tuple together appropriately and wrap in an optional, and then the user can do whatever they like with that value. The typing rule would be very much tied to the form of the pattern, but I think that's what you'd expect.

Ideally the "regexp" syntax would have the same dual-use properties: if you used it in a `case`, you could bind names directly to certain matches, and otherwise you would get the matches back tupled up and wrapped in an optional.

---

<div class="post-metadata">

**Author:** ![tclementdev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tclementdev/32/13398_2.png) [@tclementdev](https://forums.swift.org/u/tclementdev)\
**Post date:** [October 8, 2020, 3:30pm UTC](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584/20 "2020-10-08T15:30:19Z")

</div>

Personally I quite dislike that backwards-reading case syntax:

```
if case .aCase = value {
    print("a case")
}

```

I'd love to be able to write the more natural:

```
if value == .aCase {
    print("a case")
}

```

Then the assignment looks very familiar as well:

```
let bool = (value == .aCase)

```

In all of the above, I don't care whether the enum has associated values or not, and the syntax is exactly the same either way.

To retrieve an associated value, I wish I could write something similar to this:

```
if value == .aCase(let associatedValue) {
    print("a case with \(associatedValue)")
}

```

[Next page](https://forums.swift.org/t/proposal-sanity-check-assigning-a-case-statement-to-a-boolean/40584.md?page=2)
