E132: Static Overriding Non-Static Members — static 멤버가 비-static 멤버를 오버라이드
E132: Static Overriding Non-Static Members — static 멤버가 비-static 멤버를 오버라이드
컴패니언 객체의 @static 멤버가 부모 트레이트나 클래스의 비-static 멤버를 오버라이드하거나 구현하려고 할 때 발생하는 에러예요.
본문
컴패니언 객체의 @static 멤버가 부모 트레이트나 클래스의 비-static 멤버를 오버라이드하거나 구현하려고 할 때 발생해요.
@static 어노테이션은 멤버를 JVM static 멤버로 컴파일되게 해줘요. static 멤버는 인스턴스의 일부가 아니고 동적 디스패치가 불가능하므로, 상속 계층(hierarchy)에 참여할 수 없어요. 그래서 @static 멤버는 부모 타입의 추상 멤버를 오버라이드하거나 구현할 수 없어요.
예시
import scala.annotation.static
trait Parent:
def value: Int
class Child
object Child extends Parent:
@static def value: Int = 42
에러
-- [E132] Syntax Error: example.scala:9:14 -------------------------------------
9 | @static def value: Int = 42
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| @static members cannot override or implement non-static ones
해결 방법
제대로 구현하려면 @static 어노테이션을 제거하면 돼요.
// Remove the @static annotation to allow proper implementation
import scala.annotation.static
trait Parent:
def value: Int
class Child
object Child extends Parent:
def value: Int = 42
아니면 트레이트를 상속하지 않고 다른 메서드 이름을 쓰는 방법도 있어요.
// Alternative: Don't extend the trait and use a different method name
import scala.annotation.static
trait Parent:
def value: Int
class Child
object Child:
@static def staticValue: Int = 42