Kotlin-在循环中解构不起作用-变量不可访问

Kotlin-在循环中解构不起作用-变量不可访问,kotlin,Kotlin,一些: 这种语法称为解构声明。它同时创建多个变量(校正,创建多个值) 分解声明也适用于for循环:当您说: for ((a, b) in collection) { ... } 让我们看一看我的列表项: @Parcelize data class MyModel( var name: String = "", var is_locked: Boolean = true, var is_one_size: Boolean = false,

一些:

这种语法称为解构声明。它同时创建多个变量(校正,创建多个值)

分解声明也适用于for循环:当您说:

for ((a, b) in collection) { ... }
让我们看一看我的列表项:

    @Parcelize
data class MyModel(
        var name: String = "",
        var is_locked: Boolean = true,
        var is_one_size: Boolean = false,
) : Parcelable
现在我已经获得了一个“MyModel”类的列表,我正试图这样循环它们:

private fun initMyModelList(model: MutableList<MyModel>) {
 //i want to access is_locked from here with destruction but i cant ? IDE telling me the type is an int but its clearly defined as a Boolean
        for((is_locked) in model){
          //what i want to do in here is access the is_locked var of the model list and change all of them in a loop. im trying to use Destructuring in loop as a conveience. why is it not working ?
//how can i make the call signature look like this--- > is_locked = true instad of model.is_locked =true 
        }
}
for (m in model) {
    with(m) {
        is_locked = true
    }
}
private fun initMyModelList(模型:可变列表){
//我想通过销毁从这里锁定访问,但我不能告诉我类型是int,但它明确定义为Boolean
对于模型中的((已锁定){
//我想在这里做的是访问模型列表的is_locked var并在一个循环中更改所有变量。我试图在循环中使用解构作为一种方便。为什么它不起作用?
//我如何使呼叫签名看起来像这样--->is_locked=true instad of model.is_locked=true
}
}

我只想在循环中调用is_locked=true而不是model.is_locked=true。如何做到这一点?

基本上,这是不可能的,因为您的代码被编译成如下内容:

for (m in models) {
    val is_locked = m.component1()
    ...
}
这意味着您创建了一个无法重新分配的本地属性。但你可以这样做:

private fun initMyModelList(model: MutableList<MyModel>) {
 //i want to access is_locked from here with destruction but i cant ? IDE telling me the type is an int but its clearly defined as a Boolean
        for((is_locked) in model){
          //what i want to do in here is access the is_locked var of the model list and change all of them in a loop. im trying to use Destructuring in loop as a conveience. why is it not working ?
//how can i make the call signature look like this--- > is_locked = true instad of model.is_locked =true 
        }
}
for (m in model) {
    with(m) {
        is_locked = true
    }
}
是的,它并不完美,但可以通过扩展方法加以改进:

fun <T> List<T>.forEachApply(block: T.() -> Unit) {
    forEach(block)
}

private fun initMyModelList(model: MutableList<MyModel>) {
    model.forEachApply { 
        is_locked = true
    }
}
fun List.forEachApply(块:T.()->单位){
forEach(块)
}
private fun initMyModelList(模型:可变列表){
model.forEachApply{
是否锁定=正确
}
}

基本上,这是不可能的,因为您的代码被编译成如下内容:

for (m in models) {
    val is_locked = m.component1()
    ...
}
这意味着您创建了一个无法重新分配的本地属性。但你可以这样做:

private fun initMyModelList(model: MutableList<MyModel>) {
 //i want to access is_locked from here with destruction but i cant ? IDE telling me the type is an int but its clearly defined as a Boolean
        for((is_locked) in model){
          //what i want to do in here is access the is_locked var of the model list and change all of them in a loop. im trying to use Destructuring in loop as a conveience. why is it not working ?
//how can i make the call signature look like this--- > is_locked = true instad of model.is_locked =true 
        }
}
for (m in model) {
    with(m) {
        is_locked = true
    }
}
是的,它并不完美,但可以通过扩展方法加以改进:

fun <T> List<T>.forEachApply(block: T.() -> Unit) {
    forEach(block)
}

private fun initMyModelList(model: MutableList<MyModel>) {
    model.forEachApply { 
        is_locked = true
    }
}
fun List.forEachApply(块:T.()->单位){
forEach(块)
}
private fun initMyModelList(模型:可变列表){
model.forEachApply{
是否锁定=正确
}
}
这种语法称为解构声明。它同时创建多个变量

它不创建多个变量,而是捕获多个值。您使用的是值,而不是引用,因为您的源代码进一步说明:

分解声明编译为以下代码:

val name = person.component1()
val age = person.component2()
最接近您需要的是此自定义扩展函数:

inline fun <E> Iterable<E>.withEach(block: E.() -> Unit) {
    forEach {
        it.block()
    }
}

在你问一个强制性的问题“为什么不包含在STDLIB?”中,考虑函数式编程通常是关于转换不可变类型的。基本上,我在这里做的是鼓励一个坏习惯

这种语法称为解构声明。它同时创建多个变量

它不创建多个变量,而是捕获多个值。您使用的是值,而不是引用,因为您的源代码进一步说明:

分解声明编译为以下代码:

val name = person.component1()
val age = person.component2()
最接近您需要的是此自定义扩展函数:

inline fun <E> Iterable<E>.withEach(block: E.() -> Unit) {
    forEach {
        it.block()
    }
}

在你问一个强制性的问题“为什么不包含在STDLIB?”中,考虑函数式编程通常是关于转换不可变类型的。基本上,我在这里做的是鼓励养成一个坏习惯。

您可以在循环中使用解构作为只读值

data class Stuff(val name: String, val other: String)

fun doStuff() {
    val stuff = Stuff("happy", "day")
    val stuffs = listOf(stuff)

    for ((name) in stuffs) {
        println(name)
    }
}
运行该方法会将“happy”打印到控制台。Baeldung展示了一个使用它的例子


数据类是不可变的,这是最佳实践,所以我会尝试将您的数据类重写为不可变的。
.copy
函数将允许您复制数据类,但要使用新的、不同的值。

您可以在循环中使用与只读值相同的解构

data class Stuff(val name: String, val other: String)

fun doStuff() {
    val stuff = Stuff("happy", "day")
    val stuffs = listOf(stuff)

    for ((name) in stuffs) {
        println(name)
    }
}
运行该方法会将“happy”打印到控制台。Baeldung展示了一个使用它的例子


数据类是不可变的,这是最佳实践,所以我会尝试将您的数据类重写为不可变的。
.copy
函数将允许您复制数据类,但会使用新的、不同的值。

@EugenPechanec啊,是的,我的错。我想我只是假设在使用数据类时是不可变的,所以我完全忽略了这一部分!非常好的信息。是的,我今天才发现这里是只读的。Thanks@EugenPechanec啊,是的,我的错。我想我只是假设在使用数据类时是不可变的,所以我完全忽略了这一部分!非常好的信息。是的,我今天才发现这里是只读的。感谢直接触摸
forEach(块)
。我不知道就方法引用而言,
t.()->Unit
(t)->Unit
是相等的。与
forEach(block)
的接触很好。我不知道就方法引用而言,
t.()->Unit
(t)->Unit
是相等的。