3

I am trying to write a function in kotlin but I am not able reassign value to function parameters ,its saying val cannot be reassigned .

class WebView{

    var homepage = "https://example.com"

    fun webViewLoad(url: String, preferredOrientation: String) {

            if (url.equals("homepage")){
                url = homepage
            }
        }
    }

when I am trying to assign a value to url = homepage .it is giving me error val cannot be reassigned , I am new to kotlin ,I do not understand what is the issue , little help will be appreciated.

Danial clarc
  • 724
  • 1
  • 7
  • 25
  • Possible duplicate of [Kotlin function parameter: Val cannot be reassigned](https://stackoverflow.com/questions/42540035/kotlin-function-parameter-val-cannot-be-reassigned) – matt freake Jul 25 '19 at 11:22

5 Answers5

3

Kotlin parameters are immutable since Kotlin M5.1 (Reference)

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 if a function declarations (namely, it creates a property). Also, we all know that mutating parameters is no good style, so writing “val” or “var” infront of a parameter in a function, catch block of for-loop is no longer allowed.

Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
3

It is giving you error "val cannot be reassigned" because Kotlin function parameters are immutable i.e "val" by default. You don't need to mention the "val" keyword for it.

Quick Solution would be:

class WebView{

    var homepage = "https://example.com"

    fun webViewLoad(url: String, preferredOrientation: String) {
            val finalUrl = if (url.equals("homepage")) homepage else url
        }
    }
2

Function parameters works like val variables that couldn't be reassigned. Here you need to add variable with conditional initialization:

fun webViewLoad(url: String, preferredOrientation: String) {
    val urlValue = if (url.equals("homepage")){
        homepage
    } else {
        url
    }
    ... //use here "urlValue" variable
}

By the way, in kotlin you don't need to use equals function to compare string: common operator == will be automatically replaced with equals in byte code.

Ircover
  • 2,406
  • 2
  • 22
  • 42
  • Hi thanks for your answer , but I do not understand , why its not working like java function .if url is variable than why i can not assign value to it ? – Danial clarc Jul 25 '19 at 11:13
  • I'd say it's something like kotlin ideology. If it is so interesting for you, you can read about it [in this post](https://stackoverflow.com/a/40563463/4762282) and comments to it. – Ircover Jul 25 '19 at 11:17
1

Kotlin function parameters are final. There is no val or final keyword because that's the default (and can't be changed). Have a look at this.

0

By default parameters passed in the function are final what you can do is to add var. Hope it helps.

fun webViewLoad(var url: String, preferredOrientation: String) {

            if (url.equals("homepage")){
                url = homepage
            }
        }