Scala String Concatenation Problem

It was quite difficult to find in my code, but as soon as I found it, I was surprised that the compiler did not catch it or did not understand why it was right.

val my_string = "abc" + "def" "ghi" 

The value of my_string turned out to be "abcdef" , since I missed the + sign after "def" . Why didn't the compiler complain and what happened to "ghi" ?

+4
source share
2 answers

The code is valid because "ghi" is a valid expression on its own.

If it is inside a function (and does not follow anything else), then "ghi" is the return value of this function. Otherwise, it is simply ignored (for example, if you wrote 42 + 23 in a line yourself).

+10
source

"ghi" is just an expression of type String, why does the compiler complain?

+1
source

All Articles