If I evaluate the following expression in Scala REPL:
scala> "1" + 1
res0: java.lang.String = 11
the returned type is: java.lang.String.
If I evaluate this similar expression:
scala> 1 + "1"
res1: String = 11
the returned type is: String.
Why the difference?
There is no difference, however it is also not a bug. In this case:
you are using built-in feature of Java to concatenate anything into a String. After all, many people convert numbers to strings using the following “idiom”:
It works in Scala as well and results with
java.lang.String– same as in Java.On the other hand:
is a bit more complex. This is translated into:
and the
+()method is taken fromInt.+method located inInt.scala:The
Stringin this context is defined inPredef.scala:which is the source of the type “difference“. As you can see both strings are in fact the same type.