Skip to content

@vararg annotation when called from Java does not compile #7212

Closed
@ekrich

Description

@ekrich

minimized code

object foo {
  def main(args: Array[String]): Unit = {
    println("Dotty")
    // calling from Java does not compile
    val config = new SimpleConfig
    config.checkValid(config, "foo")
  }
  
  import scala.annotation.varargs
  
  trait Config {
    @varargs def checkValid(reference: Config, restrictToPaths: String*): Unit
  }

  class SimpleConfig extends Config {
    // This method in Config uses @varargs so it can be called from Java with varargs
    // See https://github.com/scala/bug/issues/10658
    // Now the code goes through the Scala varargs method but we need this one for Java
    def checkValid(reference: Config, restrictToPaths: Array[String]): Unit = {
      println("called from Java checkValid")
      checkValid(reference, restrictToPaths.toIndexedSeq: _*)
    }	

    override def checkValid(reference: Config, restrictToPaths: String*): Unit = {
      println("called from Scala checkValid")
    }
  }
}

expectation

Calling from Scala works as expected compared to Scala 2. In Dotty calling from Java does not compile and it looks like it calls the Scala method based on the error.

[error] /Users/eric/workspace/sconfig/examples/java/simple-lib/src/main/java/simplelib/SimpleLibContext.java:21:1: incompatible types: java.lang.String cannot be converted to scala.collection.immutable.Seq<java.lang.String>
[error]         config.checkValid(ConfigFactory.defaultReference(), "simple-lib");

Reference used when porting https://github.com/ekrich/sconfig from Java to Scala. scala/bug#10658

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions