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

我的梦境 提交于 2020-03-14 05:59:38

问题


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?

For example, adding a bunch of strings:

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

回答1:


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"

However, if the first line of the statement is a valid statement, 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.



来源:https://stackoverflow.com/questions/44180582/how-can-i-do-a-line-break-line-continuation-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!