Back to Scala3

E124: Term Member Needs Result Type for Implicit Search

docs/_docs/reference/error-codes/E124.md

3.8.42.2 KB
Original Source

E124: Term Member Needs Result Type for Implicit Search

This error is emitted when a term member's definition requires an implicit search, but the implicit search cannot proceed without knowing the result type of the member first.

The right-hand side of the definition requires an implicit search at the highlighted position. To avoid this error, give the member an explicit type.


Example

scala
def example =
  implicit val i = implicitly[Int]

Error

scala
-- [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.
   -----------------------------------------------------------------------------

Solution

scala
// 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
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>