E117: Polymorphic Method Missing Type in Parent

This error is emitted when a structural refinement includes a polymorphic (generic) method that does not override a method in the parent type.

Structural refinement in Scala does not allow for polymorphic methods that are not already defined in the parent type.


Example

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

Error

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

Solution

// 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 }