简体   繁体   English

Val无法在kotlin中为局部变量重新分配编译时错误

[英]Val cannot be reassigned compile time error for local variable in fun in kotlin

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. 这里有趣的交换我试图用b1更改a1的值,但它显示val无法重新分配编译时错误。 if i cant change like this then how its 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 注意:这只是一个示例,可以知道为什么我无法像在java中那样重新分配局部变量

In Kotlin val declares final, read only, reference - and that is exactly what compiler error is telling you with 在Kotlin中, val声明final,read only,reference - 这正是编译器错误告诉你的

Val cannot be reassigned Val无法重新分配

Once you assign value to val , it cannot be changed. val赋值后,无法更改。 If you want to be able to reassign it you have to declare it as var 如果您希望能够重新分配它,则必须将其声明为var

In Kotlin method parameters are implicitly declared as final val , so you cannot reassign them just like you could in Java. 在Kotlin方法中,参数被隐式声明为final val ,因此您无法像在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). 由于方法参数是按值传递的,而不是通过引用传递的,因此在Kotlin中不可能实现(在Java中不可能)。 Even if you would reassign parameters inside method call original variables passed to the method would not change. 即使你在方法调用内部重新分配参数,传递给方法的原始变量也不会改变。

There are two misunderstandings here: 这里有两个误解:

First, in Kotlin all parameters are final and this cannot be changed. 首先,在Kotlin中,所有参数都是final并且不能更改。 Just as in Java a final reference cannot be changed. 就像在Java中一样, final引用不能改变。 So you get an error when trying to reassign a final or val reference. 因此,在尝试重新分配finalval引用时会出错。

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. 其次,由于您拥有对String的引用的副本,因此您的交换函数不会影响调用者的原始引用。 Your swap function wouldn't work in Java either. 您的交换功能也不适用于Java。

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

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM