question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enumeration should derive `CanEqual`

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
soronpocommented, 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.

0reactions
Laseringcommented, 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 https://github.com/lampepfl/dotty/pull/14136 PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enumeration does not derive `CanEqual` for `strictEquality ...
I started experimenting with strictEquality just recently. Once it's on it pretty much requires you to have it specified on every case class...
Read more >
[Solved]-Deriving `CanEqual` succeeds on function when it shouldn ...
Simply speaking, with derives CanEqual here you just give the compiler know that it's legal to use == or != with instances of...
Read more >
Comparing Java enum members: == or equals()?
Both are technically correct. If you look at the source code for .equals() , it simply defers to == . I use ==...
Read more >
Object Equality - Artima
In fact, it's well known in Java that hashCode and equals should always ... object can equal this one, as determined by the...
Read more >
JavaConverters - Scala 3 - EPFL
Enumeration (via asJavaEnumeration) scala.collection.mutable. ... Alternatively, the conversion methods have descriptive names and can be invoked explicitly ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found