Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala String Concatentation Problem

Tags:

scala

This was a fairly hard bug to find in my code but once i found it i was surprised the compiler didn't catch it or understand why it was valid.

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

The value of my_string ended up being "abcdef" since i missed the + sign after "def". Why didn't the compiler complain and what happened to "ghi"?

like image 358
Mark Spangler Avatar asked Dec 21 '22 21:12

Mark Spangler


2 Answers

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

If this is inside a function (and not followed by anything else) then "ghi" is the return value of that function. Otherwise it's just ignored (like if you'd written 42 + 23 on a line on its own).

like image 184
sepp2k Avatar answered Jan 04 '23 19:01

sepp2k


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

like image 41
hbatista Avatar answered Jan 04 '23 20:01

hbatista