'Why does the plus sign need to be at the end of the line instead of at the beginning on the next line?
In Kotlin, if you want an addition expression to span multiple lines, you need to put the plus sign at the end of the lines, not at the beginning. For example:
var a = 1
var b = 2
var c = 3
var d = a +
b +
c
println(d) // prints "6"
var e = a
+ b
+ c
println(e) // prints "1"
From what I can tell, this is because Kotlin is interpreting the second example as three separate statements, like this:
var e = a
+ b
+ c
But I haven't found any documentation indicating why + somevariable by itself is a valid statement. It doesn't increment the variable, and println(+somevariable) prints the same value as println(somevariable).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
