score:2

the first overloaded definition can't match because the first argument is locale, while you provided a string.

the second alternative can't match because the second argument is a vararg parameter of object, but you provided int. int is not a subtype of object (called anyref in scala). java "fixes" it by auto-boxing, but in the scala type-system there is no automatic conversion from the primitive type int to java.lang.integer.

if the java definition was generic, something like format[t](f: string, args: t*), scala would allow your call, because unbounded type parameters in scala range over primitive types as well.

score:15

use this instead:

"%d".format(2)

the string.format method is a java thing, so it's expecting input parameters that are subtypes of java.lang.object. scala's int is not a java.lang.object:

scala> val i: java.lang.object = 2
<console>:7: error: type mismatch;
 found   : int(2)
 required: java.lang.object
note: an implicit exists from scala.int => java.lang.integer, but
methods inherited from object are rendered ambiguous.  this is to avoid
a blanket implicit which would convert any scala.int to any anyref.
you may wish to use a type ascription: `x: java.lang.integer`.
       val i: java.lang.object = 2
                                 ^

to learn more about this, you should read up on scala's distinction between anyval and anyref types. java has a distinction between objects (like integer) and primitives (like int), for efficiency. scala has a similar distinction. all types extend from any, but "value types" (basically corresponding to primitives) extend from anyval and everything else extends from anyref (basically java.lang.object). read more here.

so, to use string.format you'd have to replace it with a java integer, which is an object:

string.format("%d", new java.lang.integer(2))

but don't do this; just use it the scala way, as mentioned above.


Related Query

More Query from same tag