E025: Identifier Expected — 식별자가 필요해요

E025: Identifier Expected — 식별자가 필요해요

파서(parser)가 식별자(identifier) 를 기대했는데 다른 무언가를 발견했을 때 이 에러가 나와요. 쉽게 말하면, 이름이 와야 할 자리에 유효하지 않은 토큰(token)이 쓰인 경우예요. 대표적으로 타입 애너테이션(type annotation) 자리에 유효한 식별자가 아닌 것이 들어오면 이렇게 돼요.

출처: Scala 3 Reference

본문

아래 코드를 볼게요. val a: this = ???처럼 타입 자리에 this를 썼어요. 그런데 this는 타입 이름으로 쓰일 수 있는 식별자가 아니죠. 그래서 컴파일러가 "식별자가 필요하다"고 알려주는 거예요.

예제 (Example)

object obj2:
  val a: this = ???

오류 메시지 (Error)

-- [E025] Syntax Error: example.scala:2:9 --------------------------------------
2 |  val a: this = ???
  |         ^^^^
  |         identifier expected
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An identifier expected, but this found. This could be because
  | this is not a valid identifier. As a workaround, the compiler could
  | infer the type for you. For example, instead of:
  |
  | def foo: this = {...}
  |
  | Write your code like:
  |
  | def foo = {...}
   -----------------------------------------------------------------------------

해결 방법 (Solution)

this 자리에 this.type처럼 실제로 타입으로 쓸 수 있는 표현을 넣거나, 아예 타입 추론에 맡기면 돼요.

object obj2:
  val a: this.type = ???

더 알아보기 (Learn more)