9

I want to have a simple enumDescr function for any Scala 3 enum.

Example:

  @description(enumDescr(InvoiceCategory))
  enum InvoiceCategory:
    case `Travel Expenses`
    case Misc
    case `Software License Costs`

In Scala 2 this is simple (Enumeration):

def enumDescr(enum: Enumeration): String =
  s"$enum: ${enum.values.mkString(", ")}"

But how is it done in Scala 3:

def enumDescr(enumeration: ??) = ...

3 Answers 3

12

Java Reflection

If you declare your enum as Java-compatible, you can use Java reflection to get the array of its values using the Class.getEnumConstants method.

To declare a Java-compatible enum it has to extend the Enum class:

enum Color extends Enum[Color]:
  case Red, Green, Blue

You can use getEnumConstants on the static class to get correctly typed Array of values:

val values: Array[Color] = classOf[Color].getEnumConstants

But if you want to use it in a generic manner, I believe you have to cast the Class or the Array to the correct type with asInstanceOf:

def enumValues[E <: Enum[E] : ClassTag]: Array[E] =
  classTag[E].runtimeClass.getEnumConstants.asInstanceOf[Array[E]]

Subtype declaration <: Enum[E] is not strictly needed there, but is used to avoid calling it with unrelated classes and causing runtime exceptions.

Now a method enumDescr can be written in a similar way:

def enumDescr[E <: Enum[E] : ClassTag]: String =
  val cl = classTag[E].runtimeClass.asInstanceOf[Class[E]]
  s"${cl.getName}: ${cl.getEnumConstants.mkString(", ")}"

And called like this:

scala> enumDescr[Color]
val res0: String = Color: Red, Green, Blue

Scala compile-time reflection

If you want just the names of the enum cases, you can get them using scala.deriving.Mirror (thanks to @unclebob for the idea):

import scala.deriving.Mirror
import scala.compiletime.{constValue, constValueTuple}

enum Color:
  case Red, Green, Blue

inline def enumDescription[E](using m: Mirror.SumOf[E]): String =
  val name = constValue[m.MirroredLabel]
  val values = constValueTuple[m.MirroredElemLabels].productIterator.mkString(", ")
  s"$name: $values"

@main def run: Unit =
  println(enumDescription[Color])

This prints:

Color: Red, Green, Blue

Scala 3 macro for a sequence of values

You can use Scala 3 macro to generate the call to values on the companion object.

Macro definitions from the same file can't be called, so the macro has to be placed in a separate file:

/* EnumValues.scala */

import scala.quoted.*

inline def enumValues[E]: Array[E] = ${enumValuesImpl[E]}

def enumValuesImpl[E: Type](using Quotes): Expr[Array[E]] =
  import quotes.reflect.*
  val companion = Ref(TypeTree.of[E].symbol.companionModule)
  Select.unique(companion, "values").asExprOf[Array[E]]

Then in the main file:

enum Color:
  case Red, Green, Blue

// Usable from `inline` methods:
inline def genericMethodTest[E]: String =
  enumValues[E].mkString(", ")

@main def run: Unit =
  println(enumValues[Color].toSeq)
  println(genericMethodTest[Color])
1
  • can getting the actual enum values, rather than the label strings, be done generally using compile time ops? it seems that enum cases aren't constant types, so can't be materialized using constValue/constValueTuple. Also curious why enum cases don't qualify as constant values, if anyone can explain why that is?
    – anqit
    Commented Dec 13, 2023 at 5:44
4

I don't see any common trait shared by all enum companion objects.

You still can invoke the values reflectively, though:

import reflect.Selectable.reflectiveSelectable

def descrEnum(e: { def values: Array[?] }) = e.values.mkString(",")

enum Foo:
  case Bar
  case Baz

descrEnum(Foo) // "Bar,Baz"
3
  • 4
    Seems like an improvement to be made
    – SwiftMango
    Commented Oct 28, 2021 at 5:10
  • The compiler is making structural changes (adding methods) that are not reflected in the nominal type system (no common trait), so, yeah... Commented Oct 28, 2021 at 8:51
  • 1
    Minor clarification: There is a common trait - scala.reflect.Enum, but since Scala 3 compiler does not generate values or valueOf for enums with parameters, that trait does not declare those methods, since they are not common for all possible cases.
    – Lachezar
    Commented Oct 18, 2023 at 18:46
1

You can create an inline def using a Mirror.SumOf[A] which has all the information you need.

https://docs.scala-lang.org/scala3/reference/contextual/derivation.html

1
  • 2
    Could you share a complete code sample matching OP's code?
    – Gaël J
    Commented Nov 24, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.