E117: Polymorphic Method Missing Type in Parent

E117: Polymorphic Method Missing Type in Parent (부모 타입에 없는 다형성 메서드)

구조적 세분화(structural refinement)에 부모 타입의 메서드를 오버라이드하지 않는 다형성(제네릭) 메서드가 포함될 때 나오는 에러예요.

출처: Scala 3 Reference

본문

Scala의 구조적 세분화는 부모 타입에 이미 정의되지 않은 다형성 메서드를 허용하지 않아요.

예시

type Example = AnyRef { def foo[T](x: T): T }

에러 메시지

-- [E117] Syntax Error: example.scala:1:28 -------------------------------------
1 |type Example = AnyRef { def foo[T](x: T): T }
  |                        ^^^^^^^^^^^^^^^^^^^
  |Polymorphic refinement method foo without matching type in parent type AnyRef is no longer allowed
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Polymorphic method foo is not allowed in the structural refinement of type AnyRef because
  | method foo does not override any method in type AnyRef. Structural refinement does not allow for
  | polymorphic methods.
   -----------------------------------------------------------------------------

해결 방법

다형성 메서드를 트레이트에 정의하거나, 세분화에는 다형성이 아닌 메서드를 사용하면 돼요.

// Define the polymorphic method in a trait
trait HasFoo:
  def foo[T](x: T): T

type Example = HasFoo
// Or use a non-polymorphic method in the refinement
type Example = AnyRef { def foo(x: Int): Int }