Skip to content

Conflict with Scala 3 enums #357

@ybasket

Description

@ybasket

First of all, very cool that enumeratum made it to Scala 3, thank you to all people involved!

A coworker of mine found an issue though that seems worth fixing – you can't use enumeratum on Scala 3 enums due to a naming conflict:

import enumeratum.*
import enumeratum.EnumEntry.Lowercase

enum TShirt extends EnumEntry with Lowercase {
  case Black
  case Colorful
}

object TShirt extends Enum[TShirt] {
  override def values: IndexedSeq[TShirt] = findValues
}

results in an error:

Double definition:
def values: Array[Playground.TShirt] in object TShirt at line 25 and
override def values: IndexedSeq[Playground.TShirt] in object TShirt at line 29
have the same type after erasure.

Consider adding a @targetName annotation to one of the conflicting definitions
for disambiguation.

Removing values also doesn't work as Scala 3 generates a values method that is backed by an Array (which is no IndexedSeq).

Now, one could legitimately question whether you need to mix both, but I think it would be nice if that was possible. As the compiler suggests, a @targetName (which would rename enumeratum's values on byte code level) could be enough to fix it. Or are there are good arguments on why interop should be forbidden and people forced to use the more verbose sealed trait syntax? I couldn't find anything on the PR for Scala 3 support.

There's a workaround involving a bit of boilerplate (I didn't test it 100% yet though, it may have issues):

enum TShirt extends EnumEntry with Lowercase {
  case Black
  case Colorful
}

object TShirtHelper extends Enum[TShirt] {
  override def values: IndexedSeq[TShirt] = TShirt.values.toIndexedSeq
}

object TShirt {
  export TShirtHelper.{values => _, *}
}

Scastie for demonstration: https://scastie.scala-lang.org/kJLNLIaRQPWtqjgwDs4B1g

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions