scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Seemingly unrelated polymorphic function causes "ambiguous overload error"

Open hmf opened this issue 3 years ago • 0 comments

Compiler version

Tried this with 3.2.1-RC2 and 3.2.1-RC1.

Minimized code

import scala.annotation.targetName

enum CT:
  case LOCAL_DATE, LOCAL_DATE_TIME, INT

def cv[T](i: String) =
  s"cv('$i')"

@targetName("cv_date")  
def cv[T <: CT.LOCAL_DATE.type](i: Int) =
  s"cv[LocalDate]('$i')"

@targetName("cv_date_time")  
def cv[T <: CT.LOCAL_DATE_TIME.type](i: Int) =
  s"cv[LocalDateTime]('$i')"

cv[CT.LOCAL_DATE.type] (0)
cv[CT.LOCAL_DATE_TIME.type] (1)

Test may be found in scastie

Output

cv[CT.LOCAL_DATE.type] (0)
Ambiguous overload. The overloaded alternatives of method cv in object Playground with types
 [T <: (Playground.CT.LOCAL_DATE_TIME : Playground.CT)](i: Int): String
 [T <: (Playground.CT.LOCAL_DATE : Playground.CT)](i: Int): String
both match type arguments [(Playground.CT.LOCAL_DATE : Playground.CT)] and arguments ((0 : Int))

cv[CT.LOCAL_DATE_TIME.type] (1)
Ambiguous overload. The overloaded alternatives of method cv in object Playground with types
 [T <: (Playground.CT.LOCAL_DATE_TIME : Playground.CT)](i: Int): String
 [T <: (Playground.CT.LOCAL_DATE : Playground.CT)](i: Int): String
both match type arguments [(Playground.CT.LOCAL_DATE_TIME : Playground.CT)] and arguments ((1 : Int))

Expectation

I would expect the code above to compile. If I comment out the first "unrelated" function, it does compile. This also occurs for methods.

I have also noticed the following. If I use this as the 1st function instead:

def cv[T](i: Int) =
  s"cv('$i')"

The error messages also refer to it as a possible ambiguous overload. In this case, I still think it should compile.

hmf avatar Sep 19 '22 06:09 hmf