E128: Member With Same Name As Static — static 멤버와 같은 이름의 멤버
E128: Member With Same Name As Static — static 멤버와 같은 이름의 멤버
동반 클래스(companion class)가 동반 객체(companion object)의 @static 멤버와 같은 이름의 멤버를 정의할 때 발생하는 에러예요.
본문
동반 클래스가 동반 객체의 @static 멤버와 같은 이름의 멤버를 정의할 때 발생해요.
Scala의 @static 어노테이션은 멤버를 JVM 레벨의 static 멤버로 노출시켜줘요. 동반 객체에 @static 멤버가 있을 때 동반 클래스가 같은 이름의 멤버를 가지면, 생성되는 바이트코드에서 이름 충돌이 생기게 돼요.
예시
import scala.annotation.static
class Example:
val count: Int = 1
object Example:
@static val count: Int = 0
에러
-- [E128] Syntax Error: example.scala:7:14 -------------------------------------
7 | @static val count: Int = 0
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|Companion classes cannot define members with same name as a @static member
해결 방법
충돌을 피하려면 동반 객체의 @static 멤버 이름을 바꾸면 돼요.
// Rename the @static member in the companion object to avoid conflict
import scala.annotation.static
class Example:
val count: Int = 1
object Example:
@static val defaultCount: Int = 0
아니면 동반 클래스 쪽 멤버 이름을 바꿔도 되고요.
// Alternative: Rename the member in the companion class
import scala.annotation.static
class Example:
val instanceCount: Int = 1
object Example:
@static val count: Int = 0