# Multiline arithmetic operation assignment

**URL:** <https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576>\
**Category:** Support\
**Created:** [May 6, 2024, 7:38pm UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576 "2024-05-06T19:38:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gia](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gia](https://discuss.kotlinlang.org/u/gia)\
**Post date:** [May 6, 2024, 7:38pm UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/1 "2024-05-06T19:38:44Z")

</div>

I tried the following:

```auto
            val remainingSeconds = endDate.toEpochSecond(ZoneOffset.UTC)
                - dateTime.toEpochSecond(ZoneOffset.UTC)
                + randomInt(3600, 72000) // 1 to 20 hours

```

and ended up with remainingSeconds having the wrong value (only the first line)

I think I get the why, there’s no delimiters so line break equals java’s ; the other lines are considered to be just plain statements

```auto
            val remainingSeconds = endDate.toEpochSecond(ZoneOffset.UTC);
                - dateTime.toEpochSecond(ZoneOffset.UTC);
                + randomInt(3600, 72000); // 1 to 20 hours

```

So, search fails me, what options do I have to write this correctly?, well, besides putting it all in one line, and using parentheses

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [May 6, 2024, 7:58pm UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/2 "2024-05-06T19:58:47Z")

</div>

Just put operators at the end of a line, not the beginning. It won’t look exactly as you like though.

---

<div class="post-metadata">

**Author:** ![klitos](https://avatars.discourse-cdn.com/v4/letter/k/258eb7/32.png) [@klitos](https://discuss.kotlinlang.org/u/klitos)\
**Post date:** [May 7, 2024, 8:04am UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/3 "2024-05-07T08:04:50Z")

</div>

Another way you can do it is by using parentheses:

```
        val remainingSeconds = (endDate.toEpochSecond(ZoneOffset.UTC)
            - dateTime.toEpochSecond(ZoneOffset.UTC)
            + randomInt(3600, 72000) // 1 to 20 hours
        )

```

(Some linters may be very picky about the indentation when you do this.)

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [May 7, 2024, 4:27pm UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/4 "2024-05-07T16:27:16Z")

</div>

I’m not sure which API you are sticking to, but here it is how you can write this:

```kotlin
// java.time
val remainingSeconds = Duration.between(dateTime, endDate)
	.plusSeconds((3_600L..72_000L).random())
	.seconds

// kotlinx.datetime
val remainingTime = endDate.toInstant(TimeZone.UTC) -
	dateTime.toInstant(TimeZone.UTC) +
	(3_600L..72_000L).random().seconds
val remainingSeconds = remainingTime.inWholeSeconds

```

Seems a little clucky to me when you try to use normal operators across multiple lines.

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [May 9, 2024, 8:52am UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/5 "2024-05-09T08:52:02Z")

</div>

FWIW I tried doing OP’s code in IntelliJ, and it gave me a warning that the second line is an “Unused Unary operator”. I actually didn’t think it would compile; I didn’t know Kotlin had unary operators. So I learned something new today!

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [May 9, 2024, 2:37pm UTC](https://discuss.kotlinlang.org/t/multiline-arithmetic-operation-assignment/28576/6 "2024-05-09T14:37:04Z")

</div>

> [@Skater901](#):
>
> I didn’t know Kotlin had unary operators.

Most languages have a unary minus, at least — else you couldn’t refer to e.g. `-x`. The boolean negation (‘not’) operator (`!` in Kotlin) is also unary, as are the pre-/post-increment and -decrement operators `++` and `--`, and the unary `+`.

(However, the unary `+` and `-` are the only ones which are also binary operators, so the only ones you need to take care of when line-wrapping.)
