E210: Value Class Cannot Extend Alias of AnyVal

This error occurs when attempting to define a value class that extends a type alias of AnyVal instead of extending AnyVal directly.

Value classes must directly extend AnyVal. Using a type alias introduces an indirection that the compiler cannot resolve for the special treatment value classes require.

Example

type MyAnyVal = AnyVal

class Wrapper(val value: Int) extends MyAnyVal

Error

-- [E210] Syntax Error: example.scala:3:6 --------------------------------------
3 |class Wrapper(val value: Int) extends MyAnyVal
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |A value class cannot extend a type alias (type MyAnyVal) of AnyVal

Solution

Extend AnyVal directly instead of using a type alias:

class Wrapper(val value: Int) extends AnyVal

Alternatively, if you need an abstraction, consider using an opaque type instead of a value class:

object Types:
  opaque type Wrapper = Int
  object Wrapper:
    def apply(value: Int): Wrapper = value
  extension (w: Wrapper)
    def value: Int = w