开发者

Scala String Concatentation Problem

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"
开发者_StackOverflow中文版    "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"?


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).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜