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"
?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With