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

1

3

I am trying to understand this answer: https://mcmap.net/q/442358/-how-can-i-do-a-line-break-line-continuation-in-kotlin 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?

Dimaggio answered 1/9, 2022 at 22:0 Comment(0)
D
3

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.

Dimaggio answered 1/9, 2022 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.