E109: Static Fields Only Allowed in Objects

E109: Static Fields Only Allowed in Objects (@static는 객체 안에서만)

@static 어노테이션을 object 안이 아닌 멤버에 사용했을 때 나오는 에러예요.

출처: Scala 3 Reference

본문

@static 어노테이션은 Scala object 안에서 정의된 멤버에만 적용할 수 있어요. 클래스나 트레이트에는 쓸 수 없죠.

예시

import scala.annotation.static

class Example:
  @static val count = 0

에러 메시지

-- [E109] Syntax Error: example.scala:4:14 -------------------------------------
4 |  @static val count = 0
  |  ^^^^^^^^^^^^^^^^^^^^^
  |@static value count in class Example must be defined inside a static object.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | @static members are only allowed inside objects.
   -----------------------------------------------------------------------------

해결 방법

@static 멤버를 object 안으로 옮기거나, 정적 동작이 필요 없다면 @static을 제거하면 돼요.

// Move the @static member to an object
import scala.annotation.static

class Example

object Example:
  @static val count = 0
// Or remove @static if you don't need static behavior
class Example:
  val count = 0