android/kotlin-guides

Wrapping and +/- operators

yole opened this issue · 6 comments

yole commented

The current guide says: "When a line is broken at a non-assignment operator the break comes before the symbol". Unfortunately this breaks semantics for +/- operators, which can be used in unary form. For example, this is a string concatenation:

val s = "abc" +
    "def"

This is a simple string initialization, followed by an unused expression that applies unary + to a string literal:

val s = "abc"
    + "def"

The wrapping rules should be changed to either break after all operators (preferably), or to make exceptions for +/-.

Anyone have thoughts on what should be done here?

I searched the checkstyle rule on SourceForge doc for OperatorWrap, as default, the WrapOp for this rule is using NL, as it said on the checkstyle repo, google checkstyle rule and sun also use NL too, So our project also follow this rule for java code, so as the OperatorWrap default and google or sun defined for this, the correct option is:

val s = "abc"
     + "def"
if (candition1
     && candition2)

Although the above codestyle rule is for Java, but our project is developed with both Java and Kotlin, so if the rule is different, it is very confusing for our developers..

yole commented

@Jacksgong This is not a code style issue, this is a language syntax issue. The confusion of your developers is unfortunate, but you do have to wrap after + and -, not before them, if you want the code to function correctly.

@yole okay, thanks for your response, I know that now, maybe because of there isn't ; for kotlin lang, so for the case of

val a = 1
       -2

we can't know whether a is 1 or -1 now, it very confuses.

yole commented

I'm not sure what you mean by "we can't know". You can know this; a will be 1.

Yep, I mean as the expert kotlin lang engineer, they absolutely know what does this means, but for the beginner, especially from Java, they will confusing, because on the Java, the following code is means a is -1:

int a = 1
       -2;