Kotlin function parameter: Val cannot be reassigned

Function parameters in Kotlin are basically read-only val‘s inside the function, so z here will always refer to the original object that was passed in.

If you need to modify what it points to while your function is running, you’ll have to make a local copy of it at the beginning of the function, and then you can make that a var.

For example, you could start your function like this, which lets you reassign this local var later:

fun insertFixup(_z: Node?) {
    var z = _z
    // ...
    z = z.parent
    // ...
}

Leave a Comment