Back to Scala3

E117: Polymorphic Method Missing Type in Parent

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

3.8.41.7 KB
Original Source

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

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

Error

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

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

type Example = HasFoo
scala
// Or use a non-polymorphic method in the refinement
type Example = AnyRef { def foo(x: Int): Int }
<!-- 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>