Marcel
Marcel

Reputation: 27

How to change val to var in function kotlin

I'm new to kotlin. I've got a problem with making a function that changes the value of the boolean.

fun main () {

    var x = false

    functionWithBoolean(variable = x)

}

fun functionWithBoolean(variable: Boolean) {

    if (variable){
        println("x is true, so I switch to false")
        variable = false //value of x should be changed from true to false
    }

    else if (variable == false){
        println("x is false, so I switch to true")
        variable = true //value of x should be changed from false to true
    }

}

The error I encountered: Kotlin: Val cannot be reassigned

Please for explanation how to make "variable" var, altough x is set for var.

Upvotes: 0

Views: 1216

Answers (4)

maybeJosiah
maybeJosiah

Reputation: 23

I find other answers sort of correct with this, val cannot be reassigned directly and there are some valid solutions about that. I also found that things with var properties can have those properties changed and they stay changed outside of functions.

This includes Array, List, and DoubleArray. In a function array[0] may be changed and used but you can't assign array unless it is a var property of something, use a BooleanArray for simplicity.

fun main () {

    var x = BooleanArray(1){false};

    functionWithBoolean(x)

}

fun functionWithBoolean(variable: BooleanArray) {

    if (variable[0]){
        println("x is true, so I switch to false")
        variable[0] = false //value of x should be changed from true to false
    }

    else if (variable[0] == false){
        println("x is false, so I switch to true")
        variable[0] = true //value of x should be changed from false to true
    }
}

Upvotes: 0

Tenfour04
Tenfour04

Reputation: 93834

To change a variable in a function, you would have to pass an actual variable setter, not just the value of the variable. So you would have to give your function a variable-setting parameter. This is just an example of how you could actually achieve this. In most cases, this would be considered convoluted design.

I changed "variable" to "input" because it doesn't make sense to call your function parameter a variable. You cannot directly pass variables around, only values.

fun main () {

    var x = false

    functionWithBoolean(input = x, useNewValue = { x = it })

}

fun functionWithBoolean(input: Boolean, useNewValue: (Boolean)->Unit) {

    if (input){
        println("x is true, so I switch to false")
        useNewValue(false)
    }

    else if (input== false){
        println("x is false, so I switch to true")
        useNewValue(true)
    }

}

This design is convoluted and error-prone, so usually you would simply return a new value from the function and assign it to your variable:

fun main () {

    var x = false

    x = functionWithBoolean(input = x)

}

fun functionWithBoolean(input: Boolean): Boolean {

    return if (input){
        false
    }

    else {
        true
    }

}

Or more simply:

fun functionWithBoolean(input: Boolean): Boolean {
    return !input
}

Upvotes: 1

AndrewL
AndrewL

Reputation: 3455

var x = false

In this x can be reassigned

But when you pass the value as an argument to functionWithBoolean you are passing are reference and so even if you did change it, it won't alter the source.

However, if you pass back the value as a return via this simple change, you will achieve what you want:

    fun main () {
        var x = false

        x = functionWithBoolean(variable = x)
    }

    fun functionWithBoolean(variable: Boolean): Boolean {

        if (variable){
            println("x is true, so I switch to false")
            return false //value of x should be changed from true to false
        }

        else{ // you don't need:  if (variable == false)
            println("x is false, so I switch to true")
            return true //value of x should be changed from false to true
        } 

    }

Upvotes: 1

JustSightseeing
JustSightseeing

Reputation: 3025

By passing the variable x into a function, you pass-by-value, not pass-by-reference you can imagine that a copy is passed. You might find a solution here (the same problem, posted Mar 1, 2017) or here, but normally you would not change the parameter value from a function.

source

Upvotes: 0

Related Questions