Back to Scala3

E172: Missing Implicit Argument

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

3.8.41.3 KB
Original Source

E172: Missing Implicit Argument

This error occurs when a method requires an implicit (context) parameter but no matching given instance is in scope.

Context parameters (using using or the older implicit syntax) are automatically provided by the compiler when a matching given instance is available. If none is found, this error is reported.


Example

scala
trait Show[T] {
  def show(t: T): String
}

def printIt[T: Show](t: T) = println(summon[Show[T]].show(t))

def test = printIt(42)

Error

scala
-- [E172] Type Error: example.scala:7:22 ---------------------------------------
7 |def test = printIt(42)
  |                      ^
  |No given instance of type Show[Int] was found for a context parameter of method printIt

Solution

scala
trait Show[T] {
  def show(t: T): String
}

def printIt[T: Show](t: T) = println(summon[Show[T]].show(t))

// Provide a given instance for the required type
given Show[Int] with {
  def show(t: Int) = t.toString
}

def test = printIt(42)
<!-- 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>