# Assignments as expressions

**URL:** <https://discuss.kotlinlang.org/t/assignments-as-expressions/1564>\
**Category:** Language Design\
**Created:** [March 29, 2016, 10:11am UTC](https://discuss.kotlinlang.org/t/assignments-as-expressions/1564 "2016-03-29T10:11:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![norswap](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/norswap/32/1737_2.png) [@norswap](https://discuss.kotlinlang.org/u/norswap)\
**Post date:** [March 29, 2016, 10:11am UTC](https://discuss.kotlinlang.org/t/assignments-as-expressions/1564/1 "2016-03-29T10:11:38Z")

</div>

Is there a discussion or rational to forbidding using an assignment as an expression in Kotlin?

I love the ability Kotin gives me write terse code, but this goes kind of contrary to that.

I understand the risks, but some countermeasure can be taken, like GCC that warns if you do `if (x = 3)` (in kotlin, only boolean variable would affected so `if (x = true)`).

Maybe this impacts smart casts?

There’s a discussion [here](https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339/11), but I’m not really convinced – the cases mentioned in [Micah’s last post](https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339/13) seem quite frequent to me.

Simple recent example:

```
class Dechain<T> (var head: Chain<T>? = null, var tail: Chain<T>? = null)

    ...

    override fun pushFront(e: T)
        = if (head == null) {
            head = Chain(e, null, null)
            tail = head
        } else {
            head ?. prev = Chain(e, null, head)
            head = head ?. prev
        }

    // what I really wanted to write:
    override fun pushFront(e: T)
        = if (head == null) head = tail = Chain(e, null, null)
          else head = head ?. prev = Chain(e, null, head)

    ...

```

I’d even take a weird syntax (or less weird like `:=`) or even something that triggers warnings.

Edit: closest I can get, but not near as readable:

```
fun pushFront(e: T)
    = if (head == null) Chain(e, null, null).let { head = it ; tail = it }
      else Chain(e, null, head).let { head ?. prev = it ; head = it }

```

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 29, 2016, 10:40am UTC](https://discuss.kotlinlang.org/t/assignments-as-expressions/1564/2 "2016-03-29T10:40:59Z")

</div>

Not going into philosophy of it, simple technical answer: the syntax for named arguments: `foo(a = b)` means “call foo with the parameter a having value b”.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [November 12, 2025, 4:21pm UTC](https://discuss.kotlinlang.org/t/assignments-as-expressions/1564/3 "2025-11-12T16:21:27Z")

</div>

Wrote this reply before noticing that this thread is nearly 10 years old

On the philosophy side of it, Kotlin favors functional programming not imperative programming, which means mutable state is not something it is going to favor. But of course a doubly-linked list like this is not something that is a good fit for functional programming.

Any way I might implement your method like this, which is a heck of a lot more readable than your version:

```auto
    override fun pushFront(e: T) =
        Chain(e, null, head).let {
            head?.prev = it 
            tail = tail ?: it
            head = it
        }

```
