# Unlikely-to-suceed pitch: remove implicit Hashable synthesis for enums without associated values

**URL:** <https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997>\
**Category:** Pitches\
**Created:** [November 14, 2018, 9:31pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997 "2018-11-14T21:31:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [November 14, 2018, 9:31pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/1 "2018-11-14T21:31:51Z")

</div>

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 is a code size cost; if the enum is public, the optimizer cannot safety strip it out since it might be used from outside the module.

- It breaks resilience. In a resilient module, new enum cases can be added to non-frozen enums without breaking ABI. However, if the enum only had empty cases previously, adding a new payload case removes the Hashable conformance, which breaks ABI, unless the new case payload is itself Hashable and the developer explicitly conforms the enum to Hashable. This might not even be possible if, eg, the enum represents error codes, and the new case is an error case with a payload that is not Hashable.

I'm most concerned about the second point. Resilience is not yet a feature in Swift 5, and it's too late in the release cycle anyway, but I'm wondering if the core team would consider taking a proposal to eliminate the implicit conformance synthesis here, even though its a source breaking change.

---

<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:** [November 14, 2018, 9:38pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/2 "2018-11-14T21:38:47Z")

</div>

We could strip it out in Swift 5 mode. That's a little weird in that Swift 4 code would be less resilient by default, but it seems like the best we could do.

I agree that automatic conformance synthesis makes the cost of requiring explicit conformance quite reasonable.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [November 14, 2018, 9:40pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/3 "2018-11-14T21:40:15Z")

</div>

Does that mean we remove `Equatable` too? Or is the removal of `Equatable` implied when removing implicit `Hashable`?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [November 14, 2018, 9:40pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/4 "2018-11-14T21:40:58Z")

</div>

> [@michelf](#):
>
> Does that mean we remove `Equatable` too? Or is the removal of `Equatable` implied when removing implicit `Hashable` ?

Yeah, I'm talking about removing both behaviors.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [November 14, 2018, 9:47pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/5 "2018-11-14T21:47:57Z")

</div>

This seems like an obvious thing to do for the sake of consistency now (as long as we're willing to accept the source breakage it causes)

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [November 14, 2018, 9:53pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/6 "2018-11-14T21:53:40Z")

</div>

This seems like a great thing to do.

Would it be possible for the migrator to slap on `: Hashable/Equatable` only for the enums that are using the implicit conformance? Perhaps for such enums there could be a fixit when these enums would emit a "does not conform to Hashable/Equatable" error which the migrator could automatically apply.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 14, 2018, 9:53pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/7 "2018-11-14T21:53:40Z")

</div>

a. If there is a circumstance where few dozen extra bytes of conformance is a problem, you probably have bigger fish to fry.  
b. This doesn't seem to be a "the default is wrong" issue, so much as a "the edge case behavior is undefined" issue. (besides the part where removing equatable from more enums just seems hellish 🙂)

More generally speaking, I would be _very_ hesitant to remove a feature that has been there from day one, and in many ways is foundational to what an enum is. I know this would be source breaking for almost _all_ of my projects.

Given all the extra considerations/evolution/research in [SE-192](https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md), my guess is there are people that have thought through this issue thoroughly and have an idea of how it could/should be handled, so I'll defer on how exactly that should be handled.

However this _certainly_ is an issue that should be considered during the decision process non-exhaustive enums in Swift. Or at least something to keep in the back of my mind 🙂

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 14, 2018, 10:47pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/8 "2018-11-14T22:47:32Z")

</div>

> [@Slava\_Pestov](#):
>
> I'm most concerned about the second point. Resilience is not yet a feature in Swift 5, and it's too late in the release cycle anyway, but I'm wondering if the core team would consider taking a proposal to eliminate the implicit conformance synthesis here, even though its a source breaking change.

_If_ we make this break, then at a minimum, I think the migrator should slap on `: Equatable, Hashable` where it's currently implicit.

However, I'd also argue that any enum that is `RawRepresentable where RawValue: Equatable` should continue to be `Equatable` (and likewise for `Hashable`) _as though_ there were conditional conformances to that effect.

---

<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:** [November 14, 2018, 10:52pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/9 "2018-11-14T22:52:50Z")

</div>

If we wanted to minimize source churn, we could take a similar approach to what we did with frozen enums—no-payload enums remain implicitly hashable in "source-only" libraries, but must be made explicitly `Equatable` or `Hashable` in resilient libraries.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [November 14, 2018, 10:52pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/10 "2018-11-14T22:52:52Z")

</div>

> [@xwu](#):
>
> However, I'd also argue that any enum that is `RawRepresentable where RawValue: Equatable` should continue to be `Equatable` (and likewise for `Hashable` ) _as though_ there were conditional conformances to that effect.

Are you suggesting that if I have `struct Foo: Equatable {}` and add a `RawRepresentable` conformance to an enum with `RawValue = Foo` that this enum should be receive synthesized conformances for `Equatable` and `Hashable`? I can't think of any good rationale for that. Or are you only suggesting that this should only happen when Swift _also_ synthesizes the `RawRepresentable` conformance?

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [November 14, 2018, 10:53pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/11 "2018-11-14T22:53:58Z")

</div>

> [@Joe\_Groff](#):
>
> If we wanted to minimize source churn, we could take a similar approach to what we did with frozen enums—no-payload enums remain implicitly hashable in "source-only" libraries, but must be made explicitly `Equatable` or `Hashable` in resilient libraries.

I think there is more value in being consistent about this than there is in minimizing source churn. The migrator can add explicit opt-in conformances where necessary (which will still be synthesized).

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 14, 2018, 10:57pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/12 "2018-11-14T22:57:58Z")

</div>

> [@anandabits](#):
>
> Are you suggesting that if I have `struct Foo: Equatable {}` and add a `RawRepresentable` conformance to an enum with `RawValue = Foo` that this enum should be receive synthesized conformances for `Equatable` and `Hashable` ? I can't think of any good rationale for that. Or are you only suggesting that this should only happen when Swift _also_ synthesizes the `RawRepresentable` conformance?

I'm suggesting that `enum Foo: Int { case x = 1, y, z }` remain `Equatable`.

If the most consistent model for that would be notionally a conditional conformance `extension AnyEnum /* cannot actually be expressed in Swift today */ : Equatable where Self : RawRepresentable, RawValue : Equatable`, then let the chips fall where they may.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [November 14, 2018, 11:03pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/13 "2018-11-14T23:03:31Z")

</div>

> [@xwu](#):
>
> If the most consistent model for that would be notionally a conditional conformance `extension AnyEnum /* cannot actually be expressed in Swift today */ : Equatable where Self : RawRepresentable, RawValue : Equatable` , then let the chips fall where they may.

I don't find an idiosyncratic, inexpressible model to be the most consistent with the rest of the langue.

`: Int` and `: String` is syntactic sugar for synthesizing `RawRepresentable`. I think it would be much more reasonable to just add `Equatable` and `Hashable` to the synthesis that syntax opts into. The cool thing about this approach is that it _does not_ require `RawValue` to be `Hashable` or `Equatable` in order for the enum to get those conformances. The raw value _is not_ a part of the representation of the enum value internally and need not be used for implementing `Equatable` and `Hashable`.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 14, 2018, 11:04pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/14 "2018-11-14T23:04:16Z")

</div>

> [@anandabits](#):
>
> > If we wanted to minimize source churn, we could take a similar approach to what we did with frozen enums—no-payload enums remain implicitly hashable in "source-only" libraries, but must be made explicitly `Equatable` or `Hashable` in resilient libraries.
> 
> I think there is more value in being consistent about this than there is in minimizing source churn. The migrator can add explicit opt-in conformances where necessary (which will still be synthesized).

The principal value of this feature was in progressive disclosure. I see it much in the same category as the implicitly synthesized initializers. Without it, there's not much one can do with an enum as a beginner: "opt-in conformance" implies that the user understands protocols and conformances, which in most pedagogical approaches is considered a more advanced concept than enums.

Consider the status quo with synthesized initializers, where they are `internal` only and have no effect on API. If `internal`-only protocol conformances were possible, then it would make sense to preserve this feature of enums as though it were spelled `enum Foo: internal Equatable, internal Hashable`. It would avoid both the code size cost for public types and would avoid breaking resilience. Since (if?) we can't do that, taking a similar approach as was done with frozen enums, as Joe suggests, seems like the next best thing.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 14, 2018, 11:05pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/15 "2018-11-14T23:05:47Z")

</div>

> [@anandabits](#):
>
> `: Int` and `: String` is syntactic sugar for synthesizing `RawRepresentable` . I think it would be much more reasonable to just add `Equatable` and `Hashable` to the synthesis that syntax opts into.

Sure, I can buy into that approach too.

---

<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:** [November 15, 2018, 12:11am UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/16 "2018-11-15T00:11:19Z")

</div>

> If we wanted to minimize source churn, we could take a similar approach to what we did with frozen enums—no-payload enums remain implicitly hashable in "source-only" libraries, but must be made explicitly `Equatable` or `Hashable` in resilient libraries.

Yeah, I think this would be reasonable.

Another way of looking at this (in the long run — we don't have this concept right now) is that the conformance is `internal` in a resilient library and must be made explicitly `public` to be usable outside of it.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [November 15, 2018, 6:33am UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/17 "2018-11-15T06:33:43Z")

</div>

There has been a former discussion on this topic: [[Proposal] Explicit Synthetic Behaviour](https://forums.swift.org/t/proposal-explicit-synthetic-behaviour/6614).

I was neutral, but mildly annoyed by automatic Equatable/Hashable synthesis for enums:

> [@\[Proposal\] Explicit Synthetic Behaviour](https://forums.swift.org/t/proposal-explicit-synthetic-behaviour/6614/11):
>
> [...] implicit, automatic, and unavoidable code synthesis code can make it difficult to write some DSLs (aka Domain-Specific Languages).

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [November 15, 2018, 11:53am UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/18 "2018-11-15T11:53:02Z")

</div>

I had no idea, +1 on removing it.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [November 15, 2018, 7:50pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/19 "2018-11-15T19:50:17Z")

</div>

This is one of those things I wish I had proposed alongside SE-0185, but I didn't potentially want the whole proposal to get bogged down with a source-breaking change.

You had me at consistency and code-size reduction, but resilience is the one that pushes this completely over the finish line for me. Between `Codable`, `Equatable`, `Hashable`, and `CaseIterable`, we've continued to enforce a belief that synthesized behavior like this should be opt-in precisely for reasons like that, and I think it's absolutely worth the source break to make sure that we don't paint ourselves into a corner based on legacy behavior that, if we had it to do all over again with the knowledge we have now, would probably go a different way.

---

<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:** [November 15, 2018, 8:09pm UTC](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997/20 "2018-11-15T20:09:48Z")

</div>

I’m on board with removing the implicit synthesis for `public` enums, but I think it has value and should remain for `internal` (and below) enums.

The rationale regarding `public` enums is straightforward: we have a philosophy in Swift that nothing is made `public` by default, so if the conformance is not spelled out in source code then it should not exist publicly. Plus the active harm regarding code size and resilience, as mentioned by Slava in the original post.

As for `internal` (etc.) enums, there are several points in favor of retaining the current behavior:

**Universality**  
The only thing you can do with a simple enum is check two instances for equality. Sure, you might pass the instances to a function which does the checking, or you might store them in a collection (eg. `Set`), but at the end of the day they’re going to get compared.

**Boilerplate**  
It would be needless boilerplate to require adding `Hashable` conformance to every simple enum just to use it in the only ways it can ever possibly be used.

**Convenience**  
It would be inconvenient to require a `switch` for every enum comparison, when an `if` or `guard` would be better suited sometimes. Not to mention loop conditions and pattern-matching `where` clauses, which can’t use `switch` at all.

**Progressive disclosure**  
New developers (or those new to Swift) just want to try thing out and see how it works. They should be able to create and use simple enums in the obvious way without having to learn about protocol conformances first.

**Source compatibility**  
We already have this feature, and it is not actively harmful for non-`public` enums, so the bar for removing it from them is not met.

[Next page](https://forums.swift.org/t/unlikely-to-suceed-pitch-remove-implicit-hashable-synthesis-for-enums-without-associated-values/17997.md?page=2)
