E034: Existential Types No Longer Supported — 실존 타입은 더 이상 지원하지 않아요

E034: Existential Types No Longer Supported — 실존 타입은 더 이상 지원하지 않아요

실존 타입(existential type) 문법(forSome 사용)을 사용할 때 이 에러가 나와요. Scala 3에서는 실존 타입을 더 이상 지원하지 않기 때문이에요.

출처: Scala 3 Reference

본문

실존 타입은 Scala 2의 기능이었어요. forSome 문법을 이용해 알려지지 않은 타입을 추상화할 수 있었죠. Scala 3에서는 이 기능을 제거하고, 더 단순하고 원리적인 대안들을 쓰도록 바뀌었어요.

실존 타입 대신 이렇게 써요.

  • 와일드카드 타입: 알려지지 않은 타입 파라미터에는 ?(Scala 2 호환 모드에서는 _)를 사용한다
  • 타입 파라미터: 명시적인 타입 파라미터를 가진 제네릭 메서드나 클래스를 사용한다
  • 의존 타입: 더 고급 사용 사례에는 의존 타입(dependent type)을 사용한다

예제 (Example)

def example[T]: List[T forSome { type T }] = List()

오류 메시지 (Error)

-- [E034] Syntax Error: example.scala:1:23 -------------------------------------
1 |def example[T]: List[T forSome { type T }] = List()
  |                       ^^^^^^^
  |                       Existential types are no longer supported -
  |                       use a wildcard or dependent type instead
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The use of existential types is no longer supported.
  |
  | You should use a wildcard or dependent type instead.
  |
  | For example:
  |
  | Instead of using forSome to specify a type variable
  |
  | List[T forSome { type T }]
  |
  | Try using a wildcard type variable
  |
  | List[?]
   -----------------------------------------------------------------------------

해결 방법 (Solution)

와일드카드 타입, 타입 파라미터, 또는 타입이 중요하지 않다면 Any를 사용하면 돼요.

// Use a wildcard type
def example: List[?] = List()
// Alternative: use a type parameter
def example[T]: List[T] = List()
// Alternative: use Any if the type doesn't matter
def example: List[Any] = List()

더 알아보기 (Learn more)