0

In Android Studio, programming in Kotlin, if you write:

val t = listOf("A","B") + listOf("C","D")

everything is fine and dandy. However, if you write:

val t = listOf("A","B")
        + listOf("C","D")

the IDE will report a "Unresolved reference" in the plus sign. The compiler also won't compile. Why is the line break meaningful in this case? Am I missing something?

Julen
  • 319
  • 2
  • 9

1 Answers1

0

As shown here, you need to add parentheses or move your plus sign at the end of the first line for it to compile. Since kotlin doesn't use semi-colons, if the first line works as a valid statement, it won't connect the next line with it.

Here are your two options:

val t = (listOf("A","B")
    + listOf("C","D"))

val t = listOf("A","B") +
    listOf("C","D")

By adding the parentheses or the plus sign at the end of the first line, you're telling the compiler that the statement isn't complete. Therefore, it is going to link the next line to the statement.

DrBibop
  • 16
  • 2