Why can I concatenate String and Int in Scala?

Viewed 4274

I'm trying out some things in Scala, coming from Python. Since Scala is a lot more strict about keeping types consistent, I was surprised to find out that I can do the following concatenation, which would blow up in Python:

def adder(one:Any, two:String) = {one+two}

adder("word", "suffix")

res13: String = wordsuffix

But also:

val x:Int = 1

adder(x, "suffix")

res12: String = 1suffix

  • So it just transforms an Int into a String w/out telling me. What is this called and what is the logic behind it?

  • And what is the benefit of this? I feel it can come back to bite me, e.g. when dealing with user input to a function.

I know this is not very specific and if this is too broad, I'll gladly retract the question.

3 Answers
Related