How can I make a line break (line continuation) in Kotlin

I have a long line of code that I want to split into multiple lines. What am I using and what is the syntax?

For example, adding a group of lines:

val text = "This " + "is " + "a " + "long " + "long " + "line"

      

+3


source to share


1 answer


There is no line continuation symbol in Kotlin. Since its grammar allows spaces between almost all characters, you can simply break the statement:

val text = "This " + "is " + "a " +
        "long " + "long " + "line"

      

However, if the first line of the statement is valid, it doesn't work :

val text = "This " + "is " + "a "
        + "long " + "long " + "line" // syntax error

      



To avoid such problems when breaking long statements across multiple lines, you can use parentheses:

val text = ("This " + "is " + "a "
        + "long " + "long " + "line") // no syntax error

      

See Kotlin grammar for more information .

+10


source







All Articles