Kotlin- Jetpack Owl中的未知语法示例

发布于 2025-01-23 16:18:25 字数 277 浏览 3 评论 0原文

试图找到有关如何在懒惰列中选择多个项目的方向,我在OWL JetPack组成示例(onboarding.kt)中找到了以下代码。

...    
val (selected, onSelected) = remember { mutableStateOf(false) }
...

即使我能够自己使用代码,也无法解码该Val声明的语法。 我在kotlinlang.org网站上找不到任何东西(我发现最近的主题是关于破坏声明的)。 有人可以帮助我理解它和/或将我指向相关文档吗?

trying to find some direction on how to select multiple items in a lazy column, I have found the following code in Owl Jetpack Compose example (Onboarding.kt).

...    
val (selected, onSelected) = remember { mutableStateOf(false) }
...

Even if I'm able to use the code by myself, I really not able to decode the syntax of this val declaration.
I wasn't able to find anything in kotlinlang.org site (the nearest topic I've found is about Destructuring declarations).
Could someone help me to understand it and/or point me to relevant documentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2025-01-30 16:18:25

正如您提到的那样,该语法正在破坏它,该语法是

val (selected: Boolean, onSelected: (Boolean) -> Unit) = remember { mutableStateOf(false) }

指靶向类的2个组成部分的expclicity。
(val num1:int,val num2:int)= pair(1,2)是一个

具有mutableState的

@Stable
interface MutableState<T> : State<T> {
    override var value: T
    operator fun component1(): T
    operator fun component2(): (T) -> Unit
}

示例,您需要添加t type type和一个将t作为t作为参数和返回单元。

内部snapshotimpl mutableState的源代码,因此

override operator fun component2(): (T) -> Unit = { value = it }

您使用此lambda设置的任何东西都分配给值t

This syntax is Destructuring as you mentioned in question which is expclicitly as

val (selected: Boolean, onSelected: (Boolean) -> Unit) = remember { mutableStateOf(false) }

Which requires 2 components of targetted class.
(val num1:Int, val num2:Int) = Pair(1,2) is an example

With MutableState

@Stable
interface MutableState<T> : State<T> {
    override var value: T
    operator fun component1(): T
    operator fun component2(): (T) -> Unit
}

You need to add T type and a lambda that takes T as param and returns unit.

And inside SnapshotImpl source code of MutableState it's used as

override operator fun component2(): (T) -> Unit = { value = it }

so anything you set using this lambda is assigned to value T

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文