# \[Proposal\] Logical augmented assigment operators

**URL:** <https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398>\
**Category:** Language Design\
**Created:** [January 31, 2019, 10:51am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398 "2019-01-31T10:51:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [January 31, 2019, 10:51am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/1 "2019-01-31T10:51:07Z")

</div>

Hi there,

either I am missing out something, or it simply isn’t there. Sorry if I just missed it.

In Kotlin we have [augmented assignment operators](https://kotlinlang.org/docs/reference/operator-overloading.html#assignments) with +, -, /, \* and %. This is great. We can do this:

```kotlin
var counter = 0
for(i in 0..10)
{
    counter += 2 * i
}

```

But there are no augmented logical assignment operators ([available in Java](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26)), to do this:

```kotlin
var everythingFine = true
var individualOks = arrayOf(true, true, true, true, true, false, true)

// Check if every individual is fine
for(ok in individualOks)
{
    everythingFine &= ok
}

println(everything fine) // prints ~$ false

```

This would be great at least for (the ones, Java supports, too)

- `|=` or
- `&=` and

Additionally, you could think about adding

- `!&=` not and
- `xor=` exclusive or

They could be defined this way:

```kotlin
operator fun andAssign(other: Boolean) { this = this && other }

operator fun nandAssign(other: Boolean) { this = !(this && other) }

operator fun orAssign(other: Boolean) { this = this || other }

operator fun xorAssign(other: Boolean) { this = this xor other }

operator fun norAssign(other: Boolean) { this = !(this || other) }

```

Thanks for your interest 🙂  
Cheers,  
Gem

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [January 31, 2019, 11:03am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/2 "2019-01-31T11:03:35Z")

</div>

Off-topic: There is a function to test if a predicate holds for all elements of an iterable: [all - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/all.html)

Your example code can also be written as:

```auto
val everythingFine = individualOks.all { it }

```

---

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [January 31, 2019, 11:07am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/3 "2019-01-31T11:07:33Z")

</div>

Thanks. I know. Just wanted to give a very simple example for the augmented operators 😉

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [January 31, 2019, 1:10pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/4 "2019-01-31T13:10:43Z")

</div>

`nand`, `nor` and `xor` are not operators, regarding your proposal I don’t see the use case.

---

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [February 1, 2019, 10:51pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/5 "2019-02-01T22:51:12Z")

</div>

Even if they are not operators, they will be useful. At least `&=` and `|=` should be beyond question.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 1, 2019, 11:58pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/6 "2019-02-01T23:58:23Z")

</div>

I might be wrong here, but don’t `&=` and `|=` normally represent the bitwise operation and not the boolean operation. This would make kotlin very inconsistent, having bitwise assignment operators but no normal bitwise operators.  
So I don’t think they should be a part of kotlin as long as we don’t have bitwise operators. Whether they are a good idea is another discussion (and I’m not sure if I like them or not).

---

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [February 2, 2019, 12:00am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/7 "2019-02-02T00:00:26Z")

</div>

That’s the Java notion. To keep it consistent `&&=` and `||=` could be an alternative.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 2, 2019, 9:20am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/8 "2019-02-02T09:20:04Z")

</div>

I strongly believe Java is using the bitwise Boolean operators for &= and |=.

---

<div class="post-metadata">

**Author:** ![SOF3](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sof3/32/5646_2.png) [@SOF3](https://discuss.kotlinlang.org/u/SOF3)\
**Post date:** [February 2, 2019, 9:29am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/9 "2019-02-02T09:29:04Z")

</div>

What about a generic assignment operator for infix operators?

```kotlin
val a: Foo
val b: Bar
a fn= b

```

```kotlin
class Foo{
    infix fun fn(b: Bar): Foo
}
```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 2, 2019, 2:00pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/10 "2019-02-02T14:00:18Z")

</div>

Do you have actual example where you use it more than twice in one piece of code?

---

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [February 4, 2019, 11:06am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/11 "2019-02-04T11:06:54Z")

</div>

I use it quite often, to check whether requirements in a complex context are met. I think the most useful aspect is shortness and tidier code. It’s the same like writing

```kotlin
a1 = a1 + a2

```

instead of

```kotlin
a1 += a2

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [February 4, 2019, 11:11am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/12 "2019-02-04T11:11:12Z")

</div>

Checks do not usually require assignment. Could you give a reference to actual piece of code?

---

<div class="post-metadata">

**Author:** ![lion.d.gem.heart](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lion.d.gem.heart/32/6020_2.png) [@lion.d.gem.heart](https://discuss.kotlinlang.org/u/lion.d.gem.heart)\
**Post date:** [February 4, 2019, 11:28am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/13 "2019-02-04T11:28:57Z")

</div>

I’ll let you know, as soon as I stumble upon it again. Just noticed, that I had to replace lots of &= and |= occurences when porting my projects from Java 1.8 to Kotlin. Mostly simple assignments like:

```java
private boolean teamFit = true;

public void OnAttack(Monster defender, int damage)
{
    teamFit &= defender.isFit()
}

```

---

<div class="post-metadata">

**Author:** ![ark1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@ark1](https://discuss.kotlinlang.org/u/ark1)\
**Post date:** [February 11, 2019, 2:06am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/14 "2019-02-11T02:06:25Z")

</div>

I don’t believe that `a &&= b` notation is a good idea.  
If it would have short-circuiting behaviour, it may be not obvious that `f()` will not be evaluated in `a &&= f()` when `a` is `false`.  
If there’s no short-circuiting behaviour, `a &&= f()` won’t be the same as `a = a && f()`.

---

<div class="post-metadata">

**Author:** ![ixchel](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@ixchel](https://discuss.kotlinlang.org/u/ixchel)\
**Post date:** [March 12, 2019, 1:20am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/15 "2019-03-12T01:20:19Z")

</div>

I tried to use the |= operator in Kotlin yesterday (as I have been accustomed to doing in C, C++, and Java) and was surprised to find that it didn’t exist. The fact that &= and |= don’t exist seems like a strange omission to me, and just one more thing that I have to remember when comparing Kotlin to Java, C, and C++. I don’t see an advantage to leaving these out when +=, -=, etc. operators exist. Booleans are just as important as integers for many programs, and I think they should be able to be accessed in the same ways.

---

<div class="post-metadata">

**Author:** ![tjw.nz](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tjw.nz](https://discuss.kotlinlang.org/u/tjw.nz)\
**Post date:** [June 10, 2022, 11:31am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/17 "2022-06-10T11:31:26Z")

</div>

My experience is that boolean augmented-assignments can quite commonly be useful handling pluralities eg. in framework code & data structures.

```auto
var changed = false
for (it in items) {
    changed |= add(it)
}

```

Does Kotlin have such clear and concise meaning in an existing syntax?

There was an OT suggestion of `Iterable.all()`, but that’s too high-level for framework use; 1) in terms of already implementing short-circuiting behavior, and 2) performance overhead. I would be open to considering an |= and perhaps &= operator.

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [June 10, 2022, 11:54am UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/18 "2022-06-10T11:54:36Z")

</div>

`changed = items.firstOrNull { add(it) } != null`  
should do.

It might not be the most idiomatic Kotlin ever, however you can write your own extension method to do that, and give it a name you like.

---

<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:** [June 11, 2022, 11:35pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/19 "2022-06-11T23:35:25Z")

</div>

@al3c Your code does something different, though.

The `for` loop will call `add()` for _all_ items in the list (and set `changed` if any of them return true).

However, the `firstOrNull()` will probably stop after the first `add()` call that returns true; it _won’t_ add any remaining items.

Of course, if `add()` had no side-effects, then the end result would be the same in both cases… But it’s a fair bet that it _does_ have side-effects — and in fact those side-effects are probably its main purpose; if so, the ‘optimisation’ of `firstOrNull()` would be a big problem here!

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [June 13, 2022, 1:24pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/20 "2022-06-13T13:24:45Z")

</div>

I thought OP wanted short-circuiting.

If you don’t want it it’s even easier:  
`changed = items.map{ add(it) }.any()`

---

<div class="post-metadata">

**Author:** ![jacek.s.gajek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jacek.s.gajek/32/8517_2.png) [@jacek.s.gajek](https://discuss.kotlinlang.org/u/jacek.s.gajek)\
**Post date:** [June 13, 2022, 4:53pm UTC](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398/21 "2022-06-13T16:53:21Z")

</div>

I think you meant

```auto
changed = items.map { add(it) }.any { it }

```

🙂  
[false].any() == true.

[Next page](https://discuss.kotlinlang.org/t/proposal-logical-augmented-assigment-operators/11398.md?page=2)
