E163: Override Type Mismatch Error

Note: This error code was made inactive in Scala 3.3.0. Override type mismatch errors are now reported as E164 (Override Error).

This error occurred until Scala 3.2.x when a method override had an incompatible type signature compared to the method it was overriding. The overriding method's return type must be a subtype of the overridden method's return type (covariance).

In Scala 3.3.0, the implementation of override errors was unified, and this error code was merged into E164, which provides more comprehensive override validation and better error messages.


Example

class Base:
  def foo: Int = 42

class Derived extends Base:
  override def foo: String = "hello"

Error

-- [E163] Declaration Error: example.scala:5:15 --------------------------------
5 |  override def foo: String = "hello"
  |               ^
  |               error overriding method foo in class Base of type => Int;
  |                 method foo of type => String has incompatible type
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | I tried to show that
  |   => String
  | conforms to
  |   => Int
  | but the comparison trace ended with `false`:
  |
  |   ==> => String  <:  => Int
  |     ==> String  <:  Int
  |       ==> String  <:  Int
  |       <== String  <:  Int = false
  |     <== String  <:  Int = false
  |   <== => String  <:  => Int = false
  |
  | The tests were made under the empty constraint
   -----------------------------------------------------------------------------

Solution

class Base:
  def foo: Int = 42

class Derived extends Base:
  // Return type must be Int or a subtype
  override def foo: Int = 100
// If different behavior is needed, use a different method name
class Base:
  def foo: Int = 42

class Derived extends Base:
  def fooString: String = "hello"

See Also