kotlinline-continuation

Why no line break before `+` but before `.` in Kotlin?


I am trying to understand this answer: https://stackoverflow.com/a/44180583/481061 and particularly this part:

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

This does not seem to be the case for the dot operator:

val text = obj
           .getString()

How does this work? I'm looking at the grammar (https://kotlinlang.org/docs/reference/grammar.html) but am not sure what to look for to understand the difference. Is it built into the language outside of the grammar rules, or is it a grammar rule?


Solution

  • It is a grammar rule, but I was looking at an incomplete grammar.

    In the full grammar https://github.com/Kotlin/kotlin-spec/blob/release/grammar/src/main/antlr/KotlinParser.g4 it's made clear in the rules for memberAccessOperator and identifier.

    The DOT can always be preceded by NL* while the other operators cannot, except in parenthesized contexts which are defined separately.