Skip to content

Enumeration should derive CanEqual #13722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
soronpo opened this issue Oct 9, 2021 · 10 comments
Open

Enumeration should derive CanEqual #13722

soronpo opened this issue Oct 9, 2021 · 10 comments

Comments

@soronpo
Copy link
Contributor

soronpo commented Oct 9, 2021

When using an enumeration, we expect pattern matching on the objects to work by default even under strictEquality.

Compiler version

v3.1.0-RC1

Minimized code

import scala.language.strictEquality
enum FooBar:
  case Foo, Bar

def check(fb : FooBar): Unit =
  fb match
    case FooBar.Foo =>
    case FooBar.Bar =>

Output

Values of types FooBar and FooBar cannot be compared with == or !=
Values of types FooBar and FooBar cannot be compared with == or !=

Expectation

No error.

@soronpo
Copy link
Contributor Author

soronpo commented Oct 9, 2021

If it's not an actual bug, then it's at least missing proper documentation under the enum/equality section.

@soronpo
Copy link
Contributor Author

soronpo commented Oct 10, 2021

I noticed that the strictEquality tests explicitly derive CanEqual for enums. Therefore I conclude that this is not an overlook, but an intended design decision, and we are missing documentation under the enum and equality sections.

@bishabosha
Copy link
Member

note that case classes and case objects also do not derive CanEqual by default

@jducoeur
Copy link
Contributor

True, but I'm with @soronpo here: I believe any reasonable programmer would expect this to just work, and I'm having trouble coming up with a reasonable argument why one would ever not want this to be true. IMO, this one's a design bug, and should be tweaked.

@bishabosha
Copy link
Member

bishabosha commented Oct 11, 2021

True, but I'm with @soronpo here: I believe any reasonable programmer would expect this to just work, and I'm having trouble coming up with a reasonable argument why one would ever not want this to be true. IMO, this one's a design bug, and should be tweaked.

they did before 0.13.0-RC1, but was removed by #5843

@soronpo
Copy link
Contributor Author

soronpo commented Oct 11, 2021

@bishabosha
Copy link
Member

bishabosha commented Oct 11, 2021

Paraphrasing @smarter It would potentially be problematic if you define custom equals for your enum, and then that case should probably not derive CanEqual

@soronpo
Copy link
Contributor Author

soronpo commented Oct 11, 2021

Paraphrasing @smarter It would potentially be problematic if you define custom equals for your enum, and then that case should probably not derive CanEqual

IMO, if you define an enum you want to do basic patten matching. If not, then you don't want an enum.

@jducoeur
Copy link
Contributor

Right -- enum is syntax sugar for the common cases. There are all sorts of edge cases where it isn't quite appropriate, but I would guess that custom equals is one of the more unusual ones. (Like, I don't recall ever doing so for an enumeration, personally.)

@Lasering
Copy link

Lasering commented Mar 29, 2022

Somewhat related enums whose cases are singletons should also derive Ordering with this implementation:

given Ordering[T] = Ordering.by(_.ordinal)

Alternatively add the above code to the SingletonEnumCompanion in the #14136 PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants