E099: Only Functions Can Be Followed By Underscore

E099: Only Functions Can Be Followed By Underscore (언더스코어는 함수 뒤에만)

eta-확장(eta-expansion) 문법 x _를 메서드가 아닌 표현식에 사용할 때 나오는 에러예요.

출처: Scala 3 Reference

본문

메서드를 함수 값으로 바꿔주는 x _ 문법은 이제 비권장(deprecated)이며, 원래 메서드에만 적용돼요. 메서드가 아닌 일반 표현식은 함수 값을 만들기 위해 () => x라고 명시적으로 적어야 해요.

예시

val x = 42
val f = x _

에러 메시지

-- [E099] Syntax Error: example.scala:2:10 -------------------------------------
2 |val f = x _
  |        ^^^
  |Only function types can be followed by _ but the current expression has type Int
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The syntax x _ is no longer supported if x is not a function.
  | To convert to a function value, you need to explicitly write () => x
   -----------------------------------------------------------------------------

해결 방법

// Use explicit function syntax
val x = 42
val f = () => x