26

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.

2
  • 7
    Just a side note: Your swap function won't do anything at all.
    – glee8e
    Commented May 26, 2017 at 10:10
  • 1
    Function arguments are local, and assignment to them (which is not prohibited in Java) won't change anything outside the function. Also, you can pass an arbitrary expression to the function, e. g. swap(9, 42). This assignment may look confusing, and thus prohibited in Kotlin.
    – Miha_x64
    Commented May 26, 2017 at 11:05

3 Answers 3

34

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.

2
  • so Kotlin dosent support reassigning of args value to be changed, for that i have create a new var temp = a1 temp = b1
    – YLS
    Commented May 26, 2017 at 11:35
  • 2
    can you correct this answer for. val is not a constant nor immutable, it is a final reference, using mutable/immutable can mislead about the contents of whatever the reference is pointing at. And his error isn't about val directly but about the parameters which are always final in Kotlin. Commented May 26, 2017 at 16:43
7

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
-1

You cannot change function parameter value, instead create new variables for swapped values:

fun swap(a1: String, b1: String) {
    val a1Swapped = b1
    val b1Swapped = a1
}
3
  • this work now if i try to change/assign the value for val a1Swapped=b1 and then a1Swapped=4 it shows the same error val cannot be reassigned
    – YLS
    Commented May 26, 2017 at 10:53
  • this swaps nothing, has no side-effect outside of the function. Commented May 26, 2017 at 16:57
  • Of course, I just showed that you cannot reassign function parameters
    – gildor
    Commented May 27, 2017 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.