Reputation: 18005
I'm taking my first steps in scala, and here is an "hello world"-like code :
package test
object Main {
def main(args: Array[String]): Unit = {
println("Blabla something : " + 3)
println("Blabla something : " , 3)
}
}
Here is the output I get :
Blabla something : 3
(Blabla something : ,3)
Why are there parentheses printed in the second line, along with the "," ? I was expecting the same output as in the first line.
Note : I tried searching for scala println parentheses and such, but all I get is posts on how to remove the parentheses from the code, not from the output.
Upvotes: 1
Views: 1448
Reputation: 76
To see this in action you could do something like this in the REPL:
scala> import scala.reflect.runtime.universe.TypeTag
scala> def printz[T](x: T)(implicit tT: TypeTag[T]) = { val v = x; println(s"$v : $tT")}
scala> printz("Blabla something : " , 3)
(Blabla something : ,3) : TypeTag[(String, Int)]
TypeTag[(String, Int)]
hints at the tuple (String, Int)
.
Upvotes: 6
Reputation: 12563
When you call
println("Blabla something : " , 3)
the following happens:
println
only expects one argument. Also, in Scala functions with one argument can be called without parentheses. That means that println
interpretes everything after it as one parameter and adds parentheses around it, and your command is getting translated as:
println(("Blabla something : " , 3))
When the argument given to println
is not a String, it is converted to a String by calling its toString
method, which exists for every object (in one or another form). For a Tuple (which is the argument given to println
in this example) the default toString
method just prints the tuple as a pair of values, surrounded with quotes, which is exactly what you see.
Upvotes: 1
Reputation: 55569
The comma has a special meaning. The compiler is interpreting the arguments of println("Blabla something : " , 3)
as a Tuple
, because of the comma. In particular, one with type (String, Int)
.
Upvotes: 2