E134: No Matching Overload

This error occurs when none of the overloaded alternatives of a method match the expected type.

When a method is overloaded (has multiple definitions with different parameter types), Scala needs to select which overload to use based on the expected type. This error appears when the expected type doesn't match any of the available overloaded signatures.


Example

object Example:
  def foo(x: Int): Int = x
  def foo(x: String): String = x

def example(): Unit =
  val f: Boolean => Boolean = Example.foo

Error

-- [E134] Type Error: example.scala:6:38 ---------------------------------------
6 |  val f: Boolean => Boolean = Example.foo
  |                              ^^^^^^^^^^^
  |None of the overloaded alternatives of method foo in object Example with types
  | (x: String): String
  | (x: Int): Int
  |match expected type Boolean => Boolean

Solution

// Use a type that matches one of the overloaded alternatives
object Example:
  def foo(x: Int): Int = x
  def foo(x: String): String = x

def example(): Unit =
  val f: Int => Int = Example.foo
// Alternative: Add an overload that matches the expected type
object Example:
  def foo(x: Int): Int = x
  def foo(x: String): String = x
  def foo(x: Boolean): Boolean = x

def example(): Unit =
  val f: Boolean => Boolean = Example.foo