E124: Term Member Needs Result Type for Implicit Search — 암시적 탐색에 결과 타입이 필요한 멤버

E124: Term Member Needs Result Type for Implicit Search — 암시적 탐색에 결과 타입이 필요한 멤버

멤버 정의가 암시적 탐색(implicit search)을 요구하는데, 탐색을 진행하기 전에 멤버의 결과 타입을 먼저 알아야 할 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

멤버 정의의 오른쪽이 암시적 탐색을 요구하지만, 탐색을 진행하려면 멤버의 결과 타입을 먼저 알아야 해요. 이 에러를 피하려면 멤버에 명시적인 타입을 붙여주면 됩니다.

예시

def example =
  implicit val i = implicitly[Int]

에러

-- [E124] Cyclic Error: example.scala:2:29 -------------------------------------
2 |  implicit val i = implicitly[Int]
  |                             ^
  |value i needs result type because its right-hand side attempts implicit search
  |
  |The error occurred while trying to compute the signature of method example
  |  which required to type the right hand side of method example since no explicit type was given
  |  which required to compute the signature of value i
  |  which required to type the right hand side of value i since no explicit type was given
  |  which required to searching for an implicit argument of type Int
  |  which required to compute the signature of value i
  |
  | Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The right hand-side of value i's definition requires an implicit search at the highlighted position.
  | To avoid this error, give `value i` an explicit type.
   -----------------------------------------------------------------------------

해결 방법

순환 의존(cyclic dependency)을 피하려면 명시적인 결과 타입을 추가하고, 해당 implicit이 바깥 스코프에서 사용 가능하도록 해주면 돼요.

// Add an explicit result type to avoid the cyclic dependency
// and ensure the implicit is available from an outer scope
given Int = 42

def example =
  val i: Int = summon[Int]
  i