Back to Scala3

E148: Typed Case Does Not Explicitly Extend Typed Enum

docs/_docs/reference/error-codes/E148.md

3.8.41.9 KB
Original Source

E148: Typed Case Does Not Explicitly Extend Typed Enum

This error occurs when both an enum class and an enum case have type parameters, but the enum case does not include an explicit extends clause.

When both the enum and its case have type parameters, the compiler cannot automatically infer how the case's type parameters relate to the enum's type parameters. An explicit extends clause is required to specify this relationship.


Example

scala
enum Container[T] {
  case Item[U](value: U)
}

Error

scala
-- [E148] Syntax Error: example.scala:2:2 --------------------------------------
2 |  case Item[U](value: U)
  |  ^
  |explicit extends clause needed because both enum case and enum class have type parameters
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Enumerations where the enum class as well as the enum case have type parameters need
  | an explicit extends.
  | for example:
  |  enum Container[T] {
  |   case Item[U](u: U) extends Container[U]
  |  }
   -----------------------------------------------------------------------------

Solution

scala
enum Container[T] {
  // Add explicit extends clause to show how type parameters relate
  case Item[U](value: U) extends Container[U]
}
scala
// Alternative: If the case doesn't need its own type parameters,
// use the enum's type parameter directly
enum Container[T] {
  case Item(value: T)
}
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>