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.
Source: stackoverflow.com
Related Query
- Scala 2.9.2 can not handle the overloaded format method of the String class - why?
- If the Nothing type is at the bottom of the class hierarchy, why can I not call any conceivable method on it?
- unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?
- Why can a method returning Unit be overridden with method returning String when return types are not explicitly given?
- Why can I use a Java lib from the Scala REPL but not from a script?
- why can not overload no argument method ,for implicit class
- Why the Scala method isInstanceOf[T] is not working
- Why I can "<:" on the type class declaration but could not use "<:<"
- Where can I find scala documentation which list all available method in String class
- Why is it not possible (in scala) to provide implementation for an abstract override method in the implementing base class
- Why scala native method can not be private
- How to write a toCSV method for scala case class that would generate the csv string for the class?
- why slick2.0 can not find the TableQuery class in playframework 2.2.1?
- Error in running Scala Program: Main method not found in class main, please define the main method
- Why Scala case class copy method parameterised only with the variables defined in the case class?
- Why Scala reports error if does not specify this.type when using chain method in inherited class
- Why does the ArrayBuilder base class not provide method interfaces?
- Why can there not be any method, member or object in scope with the same name as the implicit class
- Why does the Scala compiler disallow overloaded methods with default arguments?
- scala slick method I can not understand so far
- Why won't the Scala compiler apply tail call optimization unless a method is final?
- Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?
- Why can't a class extend traits with method of the same signature?
- Why does Scala place a dollar sign at the end of class names?
- Why does Haskell's foldr NOT stackoverflow while the same Scala implementation does?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why not make every Scala class a case class?
- Can I override a scala class method with a method from a trait?
- How can I syntax check a Scala script without executing the script and generating any class files?
- Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
More Query from same tag
- How to make gradle / intellij / play framework work together?
- Why does method "combinations" return Iterator rather than Stream in Scala?
- How can I control the Java version used with Ammonite
- Scala union types with closures
- passing Dataframe contents into sql stored procedure
- Mapping a class as a column type within a class
- Scala read first element of line separately
- C#-like signals or AS3-like events in Scala?
- Set the parallelism level for all collections in Scala 2.10?
- scala Future with callback and context switching
- Scala: how do I access a Class[?0] type?
- Monad vs sequential function calls
- SCALA Lift - AJAX form multiple values
- Liftweb WiringUI And JQueryUI Conflict
- spark executor lost failure
- Use NVL logic only on selected columns spark dataframe scala
- Pattern matching an object (Any) to a Scala Map
- Scala - refer child class in parent (generics)
- Scala Akka Typed - pipeToSelf
- Setting a dynamic value as header in subseqent request Gatling/Scala
- Num of actor instance
- How to fix SBT 0.11 not finding the main class of my application?
- Simple issue - "Error: missing argument list for method"
- Calling an inner function?
- Are there any difference calling `run.db` once and multiple times in Slick
- A better syntax for recovery from a for comprehension
- When decoding a message with Circe, is it possible to extract the invalid value from a DecodingFailure
- Scala Actors and dropped entry
- Why does this example miss one Future result?
- Scala - method not recognized in constructor