# Comparing enums without their associated values

**URL:** <https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944>\
**Category:** Pitches\
**Created:** [December 19, 2018, 6:02pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944 "2018-12-19T18:02:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [December 19, 2018, 6:02pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/1 "2018-12-19T18:02:39Z")

</div>

It would be nice if you could compare an enum without its associated values i.e a case equality check only rather than case & associated value equality check.

For example:

```
enum Foo {
    case bar(Int, String)
}

let test: Foo = .bar(1, "Hello")

let isEqualWithItsAssocValues = test == .bar(1, "Hello") // true
let isEqualWithoutItsAssocValues = test == .bar(_, _) // also true

```

This makes it really easy to compare enums if you only care about the case and not its associated values and you wouldn't have to write your own equatable conformance, which could lead to problems if you want to compare with the associated values.

If the compiler could synthesise this or allow comparison without associated values then it would be helpful.

For example, a protocol like `CaseEquatable` perhaps?

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [December 19, 2018, 6:18pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/2 "2018-12-19T18:18:22Z")

</div>

In many places (`if`, `guard`, `switch`, etc.) you can already do this:

```swift
if case .bar = test {
    // ...
}

```

But it would be nice to have a shorter way of assigning the result or passing it to a function parameter, because this is tedious:

```swift
let isEqualWithoutItsAssocValues = { if case .bar = test { return true } else { return false } }()

```

The idea of synthesized properties of the form `var isBar: Bool` [has also come up](https://forums.swift.org/t/automatically-derive-properties-for-enum-cases/10843). That is another way of solving the same problem:

```swift
let isEqualWithoutItsAssocValues = test.isBar

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [December 19, 2018, 6:51pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/3 "2018-12-19T18:51:36Z")

</div>

You could manually create a nested enum without associated values:

```swift
enum Foo {
  case bar(Int)
  case baz(String)
  
  var `case`: Case {
    switch self {
    case .bar: return .bar
    case .baz: return .baz
    }
  }
  
  enum Case {
    case bar
    case baz
  }
}

```

---

<div class="post-metadata">

**Author:** ![Dante-Broggi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dante-broggi/32/7955_2.png) [@Dante-Broggi](https://forums.swift.org/u/Dante-Broggi)\
**Post date:** [December 19, 2018, 7:53pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/4 "2018-12-19T19:53:06Z")

</div>

Linking an [older thread](https://forums.swift.org/t/comparing-enum-cases-while-ignoring-associated-values/15922) on this topic.

---

<div class="post-metadata">

**Author:** ![Charles\_Constant](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charles_constant/32/13103_2.png) [@Charles\_Constant](https://forums.swift.org/u/Charles_Constant)\
**Post date:** [December 25, 2018, 1:31am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/5 "2018-12-25T01:31:32Z")

</div>

Yes, I use the "duplicate but payloadless enum thing" occasionally. It's ugly and verbose, but wow can it ever make life easier. Swift should give us a way to implement that code _for us_.

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [February 6, 2019, 5:13pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/6 "2019-02-06T17:13:31Z")

</div>

Using the is-Syntax may be a another idea:

```
if test is .bar {
  // ...
}

```

---

<div class="post-metadata">

**Author:** ![puiz](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@puiz](https://forums.swift.org/u/puiz)\
**Post date:** [February 9, 2019, 11:33pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/7 "2019-02-09T23:33:27Z")

</div>

I have a more radical stance on this: I believe associated values currently break enums in Swift.

Enums should be about comparing cases; looking up associated values goes one step beyond.

For instance, I use an enum like this in my code:

```swift
enum Marsupial {
    case koala
    case kangaroo
    case wombat
    case other
}

```

So in addition to `switch` blocks, my code is full of lines checking for individual cases like this:

```swift
if theAnimal == .koala {...

```

Suppose I want to add the possibility to store the name in the "other" case:

```swift
enum Marsupial {
    case koala
    case kangaroo
    case wombat
    case other (name: String?)
}

```

Suddenly, my enum is broken. Equality comparisons are out of the window, and I have to use some of the least intuitive syntax I've ever seen in Swift:

```swift
if case .kangaroo = theAnimal {

```

I now have to play with the Equatable protocol to at least match the cases other than `other`, and/or come up with computed properties of the enum like `isKangaroo` and `isOther`… While I would actually still expect the following to work:

```swift
if theAnimal == .kangaroo || theAnimal == .other { ...

```

Enums are about comparing _cases_. If we also want to look into associated values, let's use the `let` syntax:

```swift
if case let .other(animalName) = theAnimal, case let .other(beastName) = theBeast, animalName == beastName { ...

```

This may be complex, but IMO it is more appropriate to be complex than comparisons ignoring associated values, which should be simple, remaining true to the nature of an enum.

---

<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:** [February 10, 2019, 4:35am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/8 "2019-02-10T04:35:00Z")

</div>

The main point of my frustration is that not everything in `switch`-`case` pair can be translated to `Bool` expression:

```swift
switch base {
case pattern1: break
default: break
    print("not pattern 1")
}

// I would expect this pattern matching operator to work,
// but it doesn't if any of the case has associatedType
if !(base ~= pattern1) {
    print("not pattern 1")
}

```

Of course the main obstacle would be `case let` pattern, but I believe anyone who wants to bind `associatedType` wouldn't use pattern matching as boolean expression since it doesn't make sense to bind if case doesn't match.

So we can extend the synthesised pattern matching operator(`~=`) to cover associatedType without variable binding, either implicitly, or at user's explicit request.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 10:17pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/9 "2019-02-11T22:17:50Z")

</div>

How about a synthesised property - `caseValue` (similar to `rawValue`). For example:

```swift
enum Foo {
  case one
  case two(Int)
}

let test1 = Foo.one
let test2 = Foo.two(3)

let value1 = test1.caseValue // Foo.one
let value2 = test2.caseValue // Foo.two

```

Could call it `case` or `caseValue` - I'm fine either way.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 10:29pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/10 "2019-02-11T22:29:00Z")

</div>

A protocol version might be better:

```swift
enum Foo {
	case one
	case two(_ three: Int)
}

protocol CaseEquatable {
	static func ==(lhs: Self, rhs: Self) -> Bool
}

// This could be either synthesised or opt-in like CaseIterable
extension Foo: CaseEquatable {
	static func ==(lhs: Foo, rhs: Foo) -> Bool {
		switch (lhs, rhs) {
		case (.one, .one): return true
		case (.two(_), .two(_)): return true
		default: return false
		}
	}
}

let test1 = Foo.two(1)
let test2 = Foo.two(3)

let isEqual = test1 == test2 // true

```

---

<div class="post-metadata">

**Author:** ![rex-remind](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rex-remind/32/449_2.png) [@rex-remind](https://forums.swift.org/u/rex-remind)\
**Post date:** [February 11, 2019, 10:32pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/11 "2019-02-11T22:32:23Z")

</div>

A compiler generated `caseValue` sounds very useful. I'm -1 on a protocol that collides with `Equatable` however.

---

<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:** [February 11, 2019, 10:35pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/12 "2019-02-11T22:35:40Z")

</div>

I'm not sure if simply stripping away the associatedValue is versatile enough, since `switch-case` can compare much more. Though this problem appears few and far between for me to say.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [February 11, 2019, 10:36pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/13 "2019-02-11T22:36:02Z")

</div>

Hmm although the problem with `caseValue` would be that you cannot have two cases with the same name, so it would be more tricky to implement the feature.

With a protocol, it can be avoided, but as you pointed out, it can cause conflict with `Equatable`. Perhaps it could be a synthesised method on an enum instead of an operator (like `isEqual()`).

So you can do:

```swift
let foo = Foo.a(1)
let bar = Foo.a(2)
let isEqual = foo.isEqual(ignoringAssocValues: bar) // true

```

(maybe we need to find a better method/param name!)

---

<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:** [February 11, 2019, 10:47pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/14 "2019-02-11T22:47:06Z")

</div>

Note also that there is also some confusing behaviour in the _status quo_.  
`~=` is not used when you use `case` pattern between 2 variables of the same type.

```swift
enum Test {
    case a, b, c
}

func ~=(_ lhs: Test, _ rhs: Test) -> Bool {
    print("Using Custom Comparison")

    return true
}

print("If match")
if Test.a ~= Test.b { }

print("If case-match")
if case Test.a = Test.b { }

print("Switch case")
switch Test.a {
case .a, .b, .c: break
}

// If match
// Using Custom Comparison
// If case-match
// Switch case

```

The code above uses custom implementation only in the first cast. Perhaps we can put generated behaviour in `~=`, as well as making `~=` more in line with other `enum` comparison.

---

<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:** [February 11, 2019, 10:50pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/15 "2019-02-11T22:50:58Z")

</div>

FWIW, a number of people agree with you on this—except that our conclusion is that enums shouldn't implicitly conform to Equatable at all. Unfortunately that'd be very source-breaking.

> [@Unlikely-to-suceed pitch: remove implicit Hashable synthesis for enums without associated values](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997):
>
> Hi all, Ever since SE-0185, Swift synthesizes Hashable and Equatable conformances for enums that explicitly declare them, as long as all payload cases are Hashable or Equatable, respectively. However there is an older feature which dates back to Swift 1, where an enum where all cases are empty implicitly gets a Hashable and Equatable conformance, in the obvious way where each case maps to an ordinal. There is no way to opt out of synthesizing this conformance. This has two downsides: There …

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [February 17, 2019, 3:05am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/16 "2019-02-17T03:05:25Z")

</div>

I think we should do it [like @lassejansen suggested](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/6): as a built-in, like `if case` and `guard case`. I don't think it should be yet another synthesized property or protocol that dumps a bunch of otherwise unneeded names into the type. And like "`if case`", I think this a general feature all sum types need, not an opt-in.

Since `is` takes a type on its right side, we need another keyword to avoid complicating the parser:

```
if myEnumValue cases .enumCase1 { /*...*/ }

```

Here, I used "`cases`" as the new operator.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [February 17, 2019, 3:42am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/17 "2019-02-17T03:42:44Z")

</div>

You can already write this:

```swift
if case .a = x { … }

```

The tricky thing is to compare two _variables_ to see if they are the same enum case, ignoring associated values. The natural spelling might be:

```swift
if case x = y { … }

```

Which currently gives an error:

```swift
Expression pattern of type 'Foo' cannot match values of type 'Foo'

```

We can make this work manually by implementing the pattern-matching operator:

```swift
extension Foo {
  static func ~= (lhs: Foo, rhs: Foo) -> Bool {
    switch (lhs, rhs) {
    case (.a, .a),
         (.b, .b),
         ... : return true
    case _: return false
    }
  }
}

```

One solution would be to have the compiler synthesize this for every enum. However, this might not be ideal.

---

<div class="post-metadata">

**Author:** ![GabrielBrodersen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gabrielbrodersen/32/16318_2.png) [@GabrielBrodersen](https://forums.swift.org/u/GabrielBrodersen)\
**Post date:** [April 11, 2022, 8:52am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/19 "2022-04-11T08:52:16Z")

</div>

I encounter this need too often, especially with nested enums. **+1**

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [April 11, 2022, 9:21am UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/20 "2022-04-11T09:21:23Z")

</div>

I believe this thread could be merged with this:

> [@\[Pitch\] Enum Quality of Life Improvements](https://forums.swift.org/t/pitch-enum-quality-of-life-improvements/52770/26):
>
> I use Stripped, since the enums are stripped of associated values. Left only with the case. I have been coding Stripped versions of enums with associated values for many years and think it would be an amazing addition to the language. I usually use Sourcery to generate the nested Stripped enum type for me.

(And yes +100)

---

<div class="post-metadata">

**Author:** ![alobaili](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@alobaili](https://forums.swift.org/u/alobaili)\
**Post date:** [December 15, 2024, 6:36pm UTC](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944/21 "2024-12-15T18:36:26Z")

</div>

Another interesting idea is to utilize macros for this. It's already mentioned here:

> [@Comparing enum cases while ignoring associated values](https://forums.swift.org/t/comparing-enum-cases-while-ignoring-associated-values/15922/11):
>
> This might be an interesting use case for a macro, which could generate a nested Cases enum with one case for each case in the outer enum but without the payloads, along with a computed property that maps the payload cases in the outer enums to the corresponding no-payload cases in the inner enum.

I don't have the knowledge to implement a macro that would achieve this, but I'm very interested in using one.

[Next page](https://forums.swift.org/t/comparing-enums-without-their-associated-values/18944.md?page=2)
