Skip to content

Failure to autobox Int's to Integer's: String.format("%02x", 255) #8661

Closed
@scabug

Description

@scabug

In pure Java, an int primitive is autoboxed with Generics into java.lang.Integer when supplied to String.format:

String.format("%02x", 255) => "ff"

Scala supports some implicit conversions, but fails to transform scala.Int to java.lang.Integer here. Attempting to run the same code in Scala results in an error about method/argument mismatch:

scala> String.format("%02x", 255)
:8: error: overloaded method value format with alternatives:
(x$1: java.util.Locale,x$2: String,x$3: Object*)String
(x$1: String,x$2: Object*)String
cannot be applied to (String, Int)
String.format("%02x", 255)

As a workaround, Scala programmers can explicitly convert the Int to an Integer:

scala> String.format("%02x", new Integer(255))
res1: String = ff

But this is a confusing experience for new Scala users coming from Java. The error message isn't particularly helpful, either. In the future, could Scala please automatically convert Int's to Integer's when a method expects an Object?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions