Back to Scala3

E226: Type Parameter Shadows Type

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

3.8.41.6 KB
Original Source

E226: Type Parameter Shadows Type

This warning is emitted when a type parameter shadows a type already in scope.

Shadowing can make code harder to read and can hide a type you intended to reference. This warning helps catch those cases early.

This warning is enabled with the -Wshadow:type-parameter-shadow compiler flag.


Example

scala
class Outer:
  trait D
  def parse[D](in: D) = in

Warning

scala
-- [E226] Naming Warning: example.scala:3:12 -----------------------------------
3 |  def parse[D](in: D) = in
  |            ^
  |     Type parameter D for method parse shadows the type defined by trait D
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A type parameter shadows another type that is already in scope.
  | This can lead to confusion and potential errors.
  | Consider renaming the type parameter to avoid the shadowing.
   -----------------------------------------------------------------------------

Solution

Rename the type parameter so it no longer shadows the existing type:

scala
class Outer:
  trait D
  def parse[D2](in: D2) = in
<!-- 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>