E018: 잘못된 단순 표현식 시작

E018: 잘못된 단순 표현식 시작 (Illegal Start Simple Expr)

이 에러는 컴파일러가 표현식을 기대하는데, 표현식을 시작할 수 없는 토큰을 만났을 때 발생해요.

출처: Scala 3 Reference

본문

표현식은 다른 문맥에서 쓰이는 키워드 같은 특정 토큰으로 시작할 수 없거나, 표현식 자체가 통째로 빠져 있을 수 있어요. 이런 경우는 흔히 다음과 같아요:

Example

val `given` = 2
def example = if true then 1 else given

Error

-- [E018] Syntax Error: example.scala:2:34 -------------------------------------
2 |def example = if true then 1 else given
  |                                  ^^^^^
  |                                  expression expected but given found
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An expression cannot start with given.
   -----------------------------------------------------------------------------

Solution

// If using identifiers that are a keywords use backticks
val `given` = 2
def example = if true then 1 else `given`
// Ensure all branches have expressions
val result =
  if true then
    "yes"
  else
    "no"
// Check for missing operands
val sum = 1 + 2  // not: val sum = 1 +

더 알아보기

  • 키워드를 식별자로 쓰고 싶을 때는 백틱(backtick)으로 감싸는 방법을 참고하세요.