Back to Scala3

E134: No Matching Overload

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

3.8.41.7 KB
Original Source

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

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

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

Error

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

scala
// 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
scala
// 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
<!-- 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>