I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
例如,添加一组字符串:
val text = "This " + "is " + "a " + "long " + "long " + "line"
I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
例如,添加一组字符串:
val text = "This " + "is " + "a " + "long " + "long " + "line"
There is no symbol for line continuation in Kotlin. As its grammar allows spaces between almost all symbols, you can just break the statement:
val text = "This " + "is " + "a " +
"long " + "long " + "line"
但是,如果语句的第一行是有效语句,则it won't work:
val text = "This " + "is " + "a "
+ "long " + "long " + "line" // syntax error
To avoid such issues when breaking long statements across multiple lines you can use parentheses:
val text = ("This " + "is " + "a "
+ "long " + "long " + "line") // no syntax error
For more information, see Kotlin Grammar.