Skip to content

Fix handling of Java varargs #8977

Closed
Closed
@michelou

Description

@michelou

Issue to be solved by pull request #8718 (smarter)

Minimized code / Output

Dotty REPL (tested with version 0.24 and newer)

Let's try 3 different ways to print out formatted strings:

$ c:\opt\dotty-0.24.0-RC1\bin\dotr
Starting dotty REPL...
scala> val pi = 3.1415926
val pi: Double = 3.1415926

scala> printf("pi = %6.4f\n", pi)
pi = 3.1416

scala> println(f"pi = $pi%6.4f")
pi = 3.1416

scala> System.out.printf("pi = %6.4f\n", pi)
1 |System.out.printf("pi = %6.4f\n", pi)
  |^^^^^^^^^^^^^^^^^
  |None of the overloaded alternatives of method printf in class PrintStream with types
  | (x$0: java.util.Locale, x$1: String, x$2: Object*): java.io.PrintStream
  | (x$0: String, x$1: Object*): java.io.PrintStream
  |match arguments (("pi = %6.4f\n" : String), (pi : Double))

Scala REPL (tested with version 2.13.2)

$ c:\opt\scala-2.13.2\bin\scala
Welcome to Scala 2.13.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_252).
Type in expressions for evaluation. Or try :help.

scala> val pi = 3.1415926
val pi: Double = 3.1415926

scala> System.out.printf("pi = %6.4f\n", pi)
pi = 3.1416
val res0: java.io.PrintStream = java.io.PrintStream@689faf79

Expectation

Same output for Dotty and Scala 2 with System.out.printf.

Workaround in Dotty : add.asInstanceOf[Object] to printf argument(s).

$ c:\opt\dotty-0.24.0-RC1\bin\dotr
Starting dotty REPL...
scala> val pi = 3.1415926
val pi: Double = 3.1415926

scala> System.out.printf("pi = %6.4f\n", pi.asInstanceOf[Object])
pi = 3.1416
val res1: java.io.PrintStream = java.io.PrintStream@2e6b379c

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions