0

I want to pattern match to the new enum type in Scala 3.

With Scala 2 this is easy, because there is an abstract class Enumeration, that you can match on, like:

obj match {
  case e: Enumeration => ...
  case other => ...
}

How can I achieve this in Scala 3 as there is no common interface?

From the answers of this related question I tried:

case v: { def values: Array[?] } => ...

This compiles with a warning but when I run publishLocal it throws an exception:

[error] (api / Compile / doc) java.lang.reflect.InvocationTargetException
pme
  • 14,156
  • 3
  • 52
  • 95

1 Answers1

0

Ok this is what I came up:

case v: scala.reflect.Enum =>
  val e:{ def values: Array[?] } = v.asInstanceOf[{ def values: Array[?] }]
  e.values.map(_.toString).toList

This works and it is possible to publish. Still no idea why it is not possible with case v: { def values: Array[?] } => ...

So better solutions and explanations still welcome.

pme
  • 14,156
  • 3
  • 52
  • 95