kotlin-guides icon indicating copy to clipboard operation
kotlin-guides copied to clipboard

Wrapping and +/- operators

Open yole opened this issue 6 years ago • 6 comments

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 +/-.

yole avatar Dec 12 '17 16:12 yole

Anyone have thoughts on what should be done here?

eventualbuddha avatar Apr 23 '18 16:04 eventualbuddha

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..

Jacksgong avatar May 11 '18 04:05 Jacksgong

@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 avatar May 11 '18 07:05 yole

@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.

Jacksgong avatar May 11 '18 07:05 Jacksgong

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

yole avatar May 11 '18 08:05 yole

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;

Jacksgong avatar May 11 '18 08:05 Jacksgong