Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalac confused which overload to pick when Java has multiple varargs overloads #8800

Closed
scabug opened this issue Aug 18, 2014 · 5 comments
Closed

Comments

@scabug
Copy link

scabug commented Aug 18, 2014

Given the following Java class:

class Funs {

  public void fun(Integer a1, Object... objs) {
    for (Object obj : objs) 
      System.out.println("obj = " + obj);
  }

  public void fun(Integer a1, String... strings) {
    for (String s : strings)
      System.out.println("string = " + s);
  }

  {
    fun(1, "a", 12); // obj = a, obj = 12
    fun(1, "b", "c"); // string = b, string = c
  }
}

Scalac gets confused about which overload to pick, as shown below. Casting to Seq[Object] resolves the issue.

  val sample = new Funs

  sample.fun(1, "a", "b")

  //  [error] /Users/ktoso/code/akka/akka-stream/src/main/scala/akka/stream/impl/Samples.scala:12: overloaded method value fun with alternatives:
  //  [error]   (a1: Integer,strings: <repeated...>[String])Unit <and>
  //  [error]   (a1: Integer,objs: <repeated...>[Object])Unit
  //  [error]  cannot be applied to (Int, Int, Int)
  //  [error]   sample.fun(1, 11, 12)
  //  sample.fun(1, 11, 12) // NOPE

  //  [error] /Users/ktoso/code/akka/akka-stream/src/main/scala/akka/stream/impl/Samples.scala:14: overloaded method value fun with alternatives:
  //  [error]   (a1: Integer,strings: <repeated...>[String])Unit <and>
  //  [error]   (a1: Integer,objs: <repeated...>[Object])Unit
  //  [error]  cannot be applied to (Int, String, Int)
  //  [error]   sample.fun(1, "a", 12) // NOPE
  //  [error]          ^
  //  sample.fun(1, "a", 12) // NOPE

  //  [error] /Users/ktoso/code/akka/akka-stream/src/main/scala/akka/stream/impl/Samples.scala:27: overloaded method value fun with alternatives:
  //  [error]   (a1: Integer,strings: <repeated...>[String])Unit <and>
  //  [error]   (a1: Integer,objs: <repeated...>[Object])Unit
  //  [error]  cannot be applied to (Int, Any)
  //  [error]   sample.fun(1, Seq("a", 12): _*) // NOPE
  //  [error]          ^
  //  sample.fun(1, Seq("a", 12): _*) // NOPE

  sample.fun(1, Seq("a", 12).asInstanceOf[Seq[Object]]: _*) // OK!

We would expect scala to properly dispatch fun(1, "a", 12) to the fun(Int, Seq Object) overload instead.

More info:

@scabug
Copy link
Author

scabug commented Aug 18, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8800?orig=1
Reporter: Konrad Malawski (konrad.malawski)
Affected Versions: 2.11.2
See #8661

@scabug
Copy link
Author

scabug commented Aug 18, 2014

@lrytz said:
Even though in Scala's type system, java.lang.Object is an alias for scala.AnyRef, when reading a classfile coming from Java, the Scala compiler maps Object to Any. Why? Probably to simplify Java interop. Discussed here on SO: http://stackoverflow.com/questions/24819225/why-does-scala-map-java-lang-object-parameters-of-java-defined-methods-to-scala

Now, the Scala compiler does NOT map Object varargs in Java classfiles to Any varargs.

public class Test {
  public void f(Object o) {
    System.out.println("obj");
  }

  public void g(Object... o) {
    System.out.println("obj...");
  }
}

scala> val t = new Test
t: Test = Test@1330b682

scala> t.f(1) // works because the Scala compiler thinks f: (Any): Unit
obj

scala> t.g(1) // Scala compiler thinks g: (AnyRef): Unit
<console>:9: error: the result type of an implicit conversion must be more specific than AnyRef
              t.g(1)
                  ^

@scabug
Copy link
Author

scabug commented Aug 18, 2014

Lukas Eder (lukaseder) said:
Interesting. I guess the g(Object...) case is really sign for a compiler bug. I wonder how type inference with generic methods plays into this kind of interoperability, i.e. when the signature is something like:

  public <T> void h(T... o) {
    System.out.println("t...");
  }

On a bytecode level, T... is really erased again to Object[], but on a language level, there is a lot of (false) type-safety and type-inference that is applied. I'm saying false, because any list of parameters will be viable, in principle.

This becomes particularly interesting when the T type has side-effects on the call site, such as:

  public <T> T h(T... o) {
    System.out.println("t...");
    return o != null && o.length > 0 ? o[0] : null;
  }

I wonder if this would produce the same behaviour in Scala as in Java, e.g. (pseudo-repl):

scala> 1 == t.h(1)
true

@scabug
Copy link
Author

scabug commented Aug 18, 2014

@som-snytt said:
Isn't this the same as String.format with primitives?

On the linked ticket, I'll contribute the old error message (from 2.9?), which said verbosely that the conversion isn't automatic, but you can "safely use 1.asInstanceOf Integer (jira munches brackets)" or whatever the wording was. There might be better words, too.

The current "failed to apply" message is somewhat obscure.

scala> foo.f(2, "a", "b")  // java methods as in the ticket
res0: String = a&b&

scala> foo.f(2, "a", 1)
<console>:12: error: overloaded method value f with alternatives:
  (x$1: Int,x$2: String*)String <and>
  (x$1: Int,x$2: Object*)String
 cannot be applied to (Int, String, Int)
              foo.f(2, "a", 1)
                  ^

scala> foo.f(2, "a", 1: Integer)
res2: String = a/1/

@som-snytt
Copy link

Scala is not confused since 2.13. The examples work as... expected.

This related ticket also links to dotty discussion.

#8344

➜  snips scala t8800.Test
obj = a
obj = 12
string = b
string = c
string = a
string = b

The Lukas example also compiles and works the way his explanation no longer anticipates.

Dotty release will be pushed out to 2025 while the SIP committee figure out how the related example should behave.

scala> object t {
     |   def f(x: Object) = 1
     |   def f(x: String*) = 2
     | }
object t

scala> t.f("")
         ^
       error: ambiguous reference to overloaded definition,
       both method f in object t of type (x: String*): Int
       and  method f in object t of type (x: Object): Int
       match argument types (String)

@SethTisue SethTisue modified the milestones: Backlog, 2.13.0 Sep 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants