Skip to content
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

Avoid the use of toString for enumEntry? #351

Open
kubukoz opened this issue Oct 4, 2022 · 4 comments
Open

Avoid the use of toString for enumEntry? #351

kubukoz opened this issue Oct 4, 2022 · 4 comments

Comments

@kubukoz
Copy link
Contributor

kubukoz commented Oct 4, 2022

Currently enumeratum has this:

trait EnumEntry {

  /** String representation of this Enum Entry.
    *
    * Override in your implementation if needed
    */
  def entryName: String = stableEntryName

  private[this] lazy val stableEntryName: String = toString
}

and it works in normal conditions, but I'd argue that toString should not be relied upon. entryName can affect serialization and toString is pretty arbitrary - we made this assumption in better-tostring, and it turns out to be incompatible with enumeratum. We have a workaround, but it's pretty inconvenient.

What do you think about the possibility of doing something else? i.e. getting the entry name based on the actual source names of the enum members (derived at compile time).

@lloydmeta
Copy link
Owner

entryName could be overridden though so that the (private) call to toString doesn’t get used; is that not sufficient?

@kubukoz
Copy link
Contributor Author

kubukoz commented Oct 7, 2022

It works, but it's pretty manual (one has to change the base trait of all their enums). Ideally it wouldn't require source-level changes.

@snilard
Copy link

snilard commented Apr 6, 2023

I would also vote for some kind of refactor that the enumEntry is not using toString. Using macro to evolve it from class name at compile time looks as very logical Scala way of doing that. Or even using getClass().getName() at runtime looks as better option than toString.

This simple thing can break Enumeratum completelly:

sealed trait MyEnum extends EnumEntry with Snakecase {
  override def toString(): String = "string"
}

@kubukoz
Copy link
Contributor Author

kubukoz commented Apr 6, 2023

@snilard getClass probably runs into problems on Scala.js / Scala Native, both of which are supported by enumeratum.

I'd vote for macros.

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

No branches or pull requests

3 participants