E134: No Matching Overload — 맞는 오버로드가 없어요

E134: No Matching Overload — 맞는 오버로드가 없어요

메서드의 오버로드 대안들 중 어느 것도 기대하는 타입과 맞지 않을 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

메서드의 오버로드 대안들 중 어느 것도 기대하는 타입과 맞지 않을 때 발생해요.

메서드가 오버로드되어 있으면(파라미터 타입이 다른 정의가 여러 개 있으면), Scala는 기대하는 타입에 따라 어떤 오버로드를 사용할지 골라야 해요. 이 에러는 기대하는 타입이 사용 가능한 오버로드 시그니처 중 어느 것과도 맞지 않을 때 나타나요.

예시

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

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

에러

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

해결 방법

오버로드 대안 중 하나와 맞는 타입을 사용하면 돼요.

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