E180: Ambiguous Extension Method — 모호한 확장 메서드

E180: Ambiguous Extension Method — 모호한 확장 메서드

같은 이름의 확장 메서드가 여러 개가 어떤 타입에 적용 가능한데, 컴파일러가 어느 것을 쓸지 결정할 수 없을 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

서로 다른 import에서 온 여러 확장 메서드가 리시버 타입에 모두 유효할 때, 호출이 모호해져서 컴파일러가 이 에러를 보고해요.

주의: 이 에러는 확장 메서드 해석 실패가 멤버 조회 에러에 감싸여 있기 때문에, 항상 E008(Not Found Error) 안의 중첩 에러로 보고돼요. E180 메시지는 자세한 에러 설명 부분에 나타나요.

예시 (Example)

object Ext1:
  extension (i: Int)
    def wow: Unit = println(i)

object Ext2:
  extension (i: Int)
    def wow: Unit = println(i * 2)

def example(): Unit =
  import Ext1.wow
  import Ext2.wow
  5.wow  // error: ambiguous extension methods

에러 (Error)

컴파일하면 E008 에러가 나오고, 그 안에 E180 모호한 확장 메서드 메시지가 중첩돼 있어요:

-- [E008] Not Found Error: example.scala:12:2 ----------------------------------
12 |  5.wow
   |  ^^^^^
   |  value wow is not a member of Int.
   |  An extension method was tried, but could not be fully constructed:
   |
   |      Ext2.wow(5)
   |
   |      failed with:
   |
   |          Ambiguous extension methods:
   |          both Ext2.wow(5)
   |          and  Ext1.wow(5)
   |          are possible expansions of 5.wow

해결 방법 (Solution)

정규화된 이름(qualified name)으로 모호함을 없애세요:

object Ext1:
  extension (i: Int)
    def wow: Unit = println(i)

object Ext2:
  extension (i: Int)
    def wow: Unit = println(i * 2)

def example(): Unit =
  import Ext1.wow
  import Ext2.wow
  Ext1.wow(5)  // explicitly call Ext1's version

또는 확장 메서드를 하나만 import 하세요:

object Ext1:
  extension (i: Int)
    def wow: Unit = println(i)

object Ext2:
  extension (i: Int)
    def wow: Unit = println(i * 2)

def example(): Unit =
  import Ext1.wow  // only import Ext1's extension
  5.wow  // unambiguous

더 알아보기

같은 이름의 확장 메서드를 여러 개 import 하면 모호해져요. 정규화된 호출로 특정하거나, 하나만 import 하면 돼요.