Override toString in scala enumeratum

1.1k Views Asked by At

Let's say I have the following enum defined:

import enumeratum._
import enumeratum.EnumEntry.Lowercase

import scala.collection.immutable

sealed trait Day extends EnumEntry with Lowercase

object Day extends Enum[Day]{
  case object MON extends Day
  case object TUE extends Day
  case object WED extends Day
  /* ... */
  override def values: immutable.IndexedSeq[Day] = findValues
}
println(Day.Mon)           // MON, but I would like to see mon instead
println(Day.Mon.entryName) // mon

I would like to override Day.toString to return the entry name instead of class name. But seems that it is not so intuitive.

What I have tried:

  1. Manually override toString to return entryName:
sealed trait Day extends EnumEntry with Lowercase {
  override def toString: String = entryName
}

But it produces StackOverflowError (related to lazy val):

Exception in thread "main" java.lang.StackOverflowError
    at example.Day$MON$.enumeratum$EnumEntry$$stableEntryName(Day.scala:17)
    at enumeratum.EnumEntry.entryName(EnumEntry.scala:21)
    at enumeratum.EnumEntry.entryName$(EnumEntry.scala:21)
    at example.Day$MON$.enumeratum$EnumEntry$Lowercase$$super$entryName(Day.scala:17)
    at enumeratum.EnumEntry$Lowercase.enumeratum$EnumEntry$Lowercase$$stableEntryName(EnumEntry.scala:122)
    at enumeratum.EnumEntry$Lowercase.enumeratum$EnumEntry$Lowercase$$stableEntryName$(EnumEntry.scala:122)
    at example.Day$MON$.enumeratum$EnumEntry$Lowercase$$stableEntryName$lzycompute(Day.scala:17)
    at example.Day$MON$.enumeratum$EnumEntry$Lowercase$$stableEntryName(Day.scala:17)
    at enumeratum.EnumEntry$Lowercase.entryName(EnumEntry.scala:120)
    at enumeratum.EnumEntry$Lowercase.entryName$(EnumEntry.scala:120)
    at example.Day$MON$.entryName(Day.scala:17)
    at example.Day.toString(Day.scala:13)
    at example.Day.toString$(Day.scala:13)
    at example.Day$MON$.toString(Day.scala:17)
    at enumeratum.EnumEntry.enumeratum$EnumEntry$$stableEntryName(EnumEntry.scala:23)
    at enumeratum.EnumEntry.enumeratum$EnumEntry$$stableEntryName$(EnumEntry.scala:23)
    at example.Day$MON$.enumeratum$EnumEntry$$stableEntryName$lzycompute(Day.scala:17)
    at example.Day$MON$.enumeratum$EnumEntry$$stableEntryName(Day.scala:17)
  1. Specify which exactly super method to use:

sealed trait Day extends EnumEntry with Lowercase {
  override def toString: String = super[Lowercase].entryName
}

But still SOE.

  1. I know it is possible with manually overriding entry name but it requires writing boilerplate code:

abstract class Day(override val entryName: String) extends EnumEntry {
  override def toString: String = entryName
}

object Day extends Enum[Day]{
  case object MON extends Day("mon")
  case object TUE extends Day("tue")
  case object WED extends Day("wed")
  /* ... */
  override def values: immutable.IndexedSeq[Day] = findValues
}
  1. It works well with StringEnum but again, I will need to write boilerplate code for literals.
0

There are 0 best solutions below