Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Val cannot be reassigned a compile time error for a local variable in fun in Kotlin

Tags:

Here in fun swap I am trying to change the value of a1 with b1, but it shows "val cannot be reassigned compile time error". If I can't change like this, then how is it possible to do?

fun swap(a1: String, b1: String) {    val temp = a1    a1 = b1    b1 = temp } 

Note: This is just a sample to know why I am not able to reassign the local variable as we can do in Java.

like image 565
YLS Avatar asked May 26 '17 09:05

YLS


People also ask

Can Val be reassigned Kotlin?

Vals cannot be reassigned (like variables declared as final in Java) while vars can be reassigned after creation. If you need to reassign the value of a variable, you will have to declare it as a var. If you do not need to reassign the variable, you can declare it as a val.

How do you reassign a variable in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.

Can functions in kotlin be assigned to variables?

Functions as variables. Functions in Kotlin are simply another data type. You can assign them to variables and constants just as you can any other type of value, such as an Int or a String . This function takes two parameters and returns the sum of their values.


2 Answers

In Kotlin val declares final, read only, reference - and that is exactly what compiler error is telling you with

Val cannot be reassigned

Once you assign value to val, it cannot be changed. If you want to be able to reassign it you have to declare it as var

In Kotlin method parameters are implicitly declared as final val, so you cannot reassign them just like you could in Java.

But the core error in your code is that you are trying to swap method parameters. Since method parameters are passed by value and not by reference what you want to achieve is impossible in Kotlin (just as well as it is impossible in Java). Even if you would reassign parameters inside method call original variables passed to the method would not change.

like image 170
Dalija Prasnikar Avatar answered Oct 09 '22 19:10

Dalija Prasnikar


There are two misunderstandings here:

First, in Kotlin all parameters are final and this cannot be changed. Just as in Java a final reference cannot be changed. So you get an error when trying to reassign a final or val reference.

Second, since you have a copy of a reference to a String, your swap function would have no affect on the caller's original references. Your swap function wouldn't work in Java either.

For example, calling your code does nothing:

val s1 = "howdy" val s2 = "goodbye" swap(s1,s2)   // Java or Kotlin, doesn't matter println(s1) println(s2)  // output: // howdy // goodbye 

And definitely calling it with literals or expressions does nothing:

swap("happy","day")  // what references is it supposed to be swapping? 

You can only swap the contents inside of an object for which you hold the same reference as the caller. To make a swap routine, you would do something like:

data class MutablePair(var one: String, var two: String)  fun swap(pair: MutablePair) {  // not thread safe           val temp = pair.one    pair.one = pair.two    pair.two = temp } 

Which you could call:

val stringies = MutablePair("howdy", "goodbye") println("${stringies.one} ${stringies.two}") swap(MutablePair() println("${stringies.one} ${stringies.two}")  // output: // howdy goodbye // goodbye howdy 
like image 26
Jayson Minard Avatar answered Oct 09 '22 19:10

Jayson Minard