Back to Scala3

E224: Override Class

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

3.8.41.6 KB
Original Source

E224: Override Class

This deprecation warning is emitted when the override modifier is used on a class or trait definition.

Using override directly on a class or trait to override a type member is deprecated. Instead, you should define the class separately and override the type member with a type alias pointing to the new class.


Example

scala
trait Base:
  type T

object Impl extends Base:
  override class T

Warning

scala
-- [E224] Syntax Deprecation Warning: example.scala:5:17 -----------------------
5 |  override class T
  |                 ^
  |                 `override` modifier is deprecated for classes and traits
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Instead of overriding a type alias with a class type, use an alias of the class.
  | For example, instead of `override class C`, use `override type C = CImpl; class CImpl`.
   -----------------------------------------------------------------------------

Solution

Define the class separately and use a type alias override:

scala
trait Base:
  type T

object Impl extends Base:
  class TImpl
  override type T = TImpl
<!-- 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>