# Mutable function parameters

**URL:** <https://discuss.kotlinlang.org/t/mutable-function-parameters/27041>\
**Category:** Language Design\
**Created:** [July 5, 2023, 2:53pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041 "2023-07-05T14:53:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohsen.shahini](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mohsen.shahini/32/10416_2.png) [@mohsen.shahini](https://discuss.kotlinlang.org/u/mohsen.shahini)\
**Post date:** [July 5, 2023, 2:53pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/1 "2023-07-05T14:53:14Z")

</div>

How can a function has mutable parameters?

---

<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:** [July 5, 2023, 4:41pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/2 "2023-07-05T16:41:03Z")

</div>

It can’t. What are you trying to achieve?

---

<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:** [July 5, 2023, 4:55pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/3 "2023-07-05T16:55:48Z")

</div>

This isn’t possible: in Kotlin, function/method parameters can’t be changed to point to different objects. (Though those objects can themselves be mutable, i.e. have their properties changed.)

In fact, early versions of Kotlin allowed parameters to be changed (like Java, C, and many other languages), but this was altered about 10 years ago. It was announced [on the JetBrains blog](https://blog.jetbrains.com/kotlin/2013/02/kotlin-m5-1/), where they said:

> The main reason is that this was confusing: people tend to think that this means passing a parameter by reference, which we do not support (it is costly at runtime). Another source of confusion is primary constructors: “val” or “var” in a constructor declaration means something different from the same thing in a function declaration (namely, it creates a property). Also, we all know that mutating parameters is not good style

(I’ve made a few trivial edits for grammar.)

See also [this previous question](https://discuss.kotlinlang.org/t/method-parameters-and-mutable-variables/333), where JetBrains reiterated that policy (and most people agreed).

My own experience agrees with that: the rare cases where it would make sense to mutable parameters (e.g. to convert them to some canonical form or replace nulls) are easily handled by creating a local variable, but the subtle bugs and confusion that can result from making them mutable is much harder to work around.

See also [these](https://stackoverflow.com/q/44109098) [questions](https://stackoverflow.com/q/73495881) [on](https://stackoverflow.com/q/40563406) StackOverflow.

---

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [July 6, 2023, 8:11pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/4 "2023-07-06T20:11:14Z")

</div>

Note that you can _shadow_ function argument, which sort of mimic mutable function parameters:

```kotlin
fun foo(bar: Int) {
    var bar = bar + 1
    bar += 1
    println(bar)
}

fun main() {
    foo(1) // prints 3
}

```

Well, I would not recommend it, but it is possible.

---

<div class="post-metadata">

**Author:** ![lucolivier](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lucolivier/32/10560_2.png) [@lucolivier](https://discuss.kotlinlang.org/u/lucolivier)\
**Post date:** [July 11, 2023, 3:34pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/5 "2023-07-11T15:34:58Z")

</div>

The solution is very simple as you consider to pass reference types to functions.

1- create a generic wrapper like this

```
data class <T> Wrapper(var value: T)

```

2- embed values within it like this

```
val myInt = Wrapper(123)
val myStr = Wrapper("Hello World!")

```

3- define your param(s) function(s) taking specialised Wrapper type or not like this, and mutating Wrapped values

```
fun mutatingParamFunc(aInt: Wrapper<Int>, aStr: Wrapper<String>, aStuff: Wrapper<*>) {
     aInt.value = 321
     aStr.value = "Hello Earth!"
}

mutatingParamFunc(myInt, myStr, myStuff)
println(myInt.value)
println(myStr.value)

// >> 321
// >> Hello Earth!

```

---

<div class="post-metadata">

**Author:** ![David-OConnor](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@David-OConnor](https://discuss.kotlinlang.org/u/David-OConnor)\
**Post date:** [December 1, 2024, 4:30pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/6 "2024-12-01T16:30:46Z")

</div>

I don’t buy the explanation for this one. It works elegantly in rust, and there is no appreciable runtime cost. Passing the entire state class (etc) just to mutate a single parameter in a function is semantically confusing, from the perspective of the function signature. And, the syntax for mutability is verbose and unwealdy.

Just let us pass an &mut reference in the function signature. Much easier to read and write; makes the function signature more general and reusable. This is because there’s no verbose creation and access syntax (e.g. MutableStateFlow creation and picking apart), and because the function would just take the variable it needs to use and mutate; not the whole wrapping class.

I will give lucoliver’s Wrapper syntax a try.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [December 1, 2024, 5:06pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/7 "2024-12-01T17:06:32Z")

</div>

> [@David-OConnor](#):
>
> It works elegantly in rust, and there is no appreciable runtime cost.

The JVM and Rust are two completely separate memory models. There is no way to pass things by reference on the JVM unless you do a memory allocation of a reference object.

---

<div class="post-metadata">

**Author:** ![David-OConnor](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@David-OConnor](https://discuss.kotlinlang.org/u/David-OConnor)\
**Post date:** [December 1, 2024, 5:34pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/8 "2024-12-01T17:34:58Z")

</div>

You’re right; I don’t know enough about the implementation to trivialize that.

At the core of it, I am surprised (And frustrated) that I can’t write a function signature like this:

```kotlin
fun simpleOperation(val: &mut MyType) {

}

```

It is easy to use, easy to reason about, and general. And seems impossible in Kotlin.

---

<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:** [December 1, 2024, 10:57pm UTC](https://discuss.kotlinlang.org/t/mutable-function-parameters/27041/9 "2024-12-01T22:57:16Z")

</div>

> [@David-OConnor](#):
>
> easy to reason about

This is very subjective. Technically speaking, there is no reason Kotlin couldn’t support this feature even if underlying JVM doesn’t. But for many people this feature is confusing, so Kotlin doesn’t support it.
