E163: 오버라이드 타입 불일치 에러예요
E163: 오버라이드 타입 불일치 에러예요 (Override Type Mismatch Error)
참고: 이 에러 코드는 Scala 3.3.0에서 비활성화됐어요. 이제 오버라이드 타입 불일치 에러는 E164(Override Error)로 보고돼요.
본문
이 에러는 Scala 3.2.x까지, 메서드 오버라이드가 오버라이드 대상 메서드와 호환되지 않는 타입 시그니처를 가질 때 발생했어요. 오버라이드하는 메서드의 반환 타입은 오버라이드 대상 메서드의 반환 타입의 하위 타입(subtype)(공변)이어야 해요.
Scala 3.3.0에서 오버라이드 에러 구현이 통합되면서, 이 에러 코드는 E164로 병합됐어요. E164는 더 포괄적인 오버라이드 검증과 더 나은 에러 메시지를 제공해요.
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"
더 알아보기
- 이 에러의 후속 코드인 E164(Override Error)를 함께 참고하세요.