21

我有一长行代码,我想在多行中分解。我用什么,语法是什么?

例如,添加一堆字符串:

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

1 回答 1

45

Kotlin 中没有续行符号。由于它的语法允许几乎所有符号之间有空格,因此您可以中断语句:

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

但是,如果语句的第一行是有效语句,它将不起作用

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

为了避免在跨多行中断长语句时出现此类问题,您可以使用括号:

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

有关更多信息,请参阅Kotlin 语法

于 2017-05-25T12:28:02.023 回答