E206: Enum May Not Be Value Class
This error occurs when attempting to define an enum that extends AnyVal. Enums cannot be value classes in Scala 3.
Value classes are a mechanism for defining types that avoid runtime object allocation, but this is incompatible with the way enums are implemented in Scala 3. Enums require a class hierarchy to represent their cases, which is not possible with value classes.
Example
enum Orientation extends AnyVal:
case North, South, East, West
Error
-- [E206] Syntax Error: example.scala:1:5 --------------------------------------
1 |enum Orientation extends AnyVal:
| ^
| class Orientation may not be a value class
Solution
Remove the extends AnyVal clause from the enum definition:
enum Orientation:
case North, South, East, West
If you need a lightweight representation for performance reasons, consider using an opaque type instead:
object Orientation:
opaque type Orientation = Int
val North: Orientation = 0
val South: Orientation = 1
val East: Orientation = 2
val West: Orientation = 3
In this article