E148: 타입 파라미터 있는 enum은 명시적 extends가 필요해요

E148: 타입 파라미터 있는 enum은 명시적 extends가 필요해요 (Typed Case Does Not Explicitly Extend Typed Enum)

enum class와 enum case가 모두 타입 파라미터를 갖는데, case에 명시적인 extends 절이 없을 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

enum과 그 case가 모두 타입 파라미터를 갖는 경우, 컴파일러는 case의 타입 파라미터가 enum의 타입 파라미터와 어떻게 연결되는지 자동으로 추론할 수 없어요. 이 관계를 명시하려면 extends 절이 꼭 필요해요.

Example

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

Error

-- [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

enum Container[T] {
  // Add explicit extends clause to show how type parameters relate
  case Item[U](value: U) extends Container[U]
}
// 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)
}

더 알아보기

  • enum과 타입 파라미터에 대한 자세한 내용은 Scala 3 Reference의 enum 문서를 참고하세요.