Missing dedicated class for enum companions

The enum classes extend a common trait scala.reflect.Enum but the enum companions do not, which makes it difficult to generalize over without macros.

I think it will be better if enum companions extend scala.reflect.EnumCompanion, that is defined as:

trait EnumCompanion[E <: scala.reflect.Enum]:
  def values : Array[E]
  def valueOf(name : String) : E
13 Likes

It’s surprising that’s not the case. Seems very reasonable to me.

6 Likes

Looks reasonable. Maybe someone wants to do a PR?

5 Likes

I’ll try to take this on.

6 Likes

Great idea, would be very useful.

Is the reason values returns an Array instead of IArray for Java compat? A lot of developers complain that Java allocates a new Array on every call to values.

1 Like

Currently, we’ve been taking the approach to not generate values and valueOf for ADT enums - so perhaps there could be two subtypes of this class - one with the methods and one without.

I also think it would be good to expose the companion to implicit search - similar to how we summon Mirror in the compiler

5 Likes

Seconded, ScalaPb does this, and it’s extremely useful

1 Like