Skip to content

Ambiguous overload when involving wildcard types and variadic parameters #24072

@SrTobi

Description

@SrTobi

Compiler version

3.7.3

Minimized code

object Test {
  def main(args: Array[String]): Unit = {
    blub(classOf[Object])
  }

  def blub[T](a: Class[? <: T]): Unit = println("a")
  def blub[T](a: Class[T], ints: Int*): Unit = println("b")
}

Output

Ambiguous overload. The overloaded alternatives of method blub in object Test with types
 [T](a: Class[T], ints: Int*): Unit
 [T](a: Class[? <: T]): Unit
both match arguments ((classOf[Object] : Class[Object]))

Expectation

Should run and print a

Reasons this should not be an error

  1. The same code compiles in Scala 2 and prints a
  2. If the variadic parameter is replaced with a default parameter, the code compiles in Scala2 and Scala3 and prints a
  3. The same code works in Java and otherwise it's not possible to call the version that prints a from Scala3:
public class TestInJava {
   public static void main(String[] args) {
       blub(Object.class);
   }

   public static <T> void blub(Class<? extends T> cls) {
       System.out.println("a");
   }

   public static <T> void blub(Class<T> cls, int ...b) {
       System.out.println("b");
   }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions