gpt4 book ai didi

android - 这个 kotlin jetpack compose 代码示例中的 "provides"语法是什么?

转载 作者:行者123 更新时间:2023-12-04 23:52:53 25 4
gpt4 key购买 nike

此代码示例中的“provides”语法是什么?它的作用是什么?

LocalContentAlpha provides ContentAlpha.medium
它似乎不是标准的 kotlin 关键字,我在谷歌上搜索“kotlin 提供关键字”或“jetpack compose 提供”之类的查询并不顺利。
这显示在 Jetpack Compose codelab ,下面的完整 fragment 。
@Composable
fun PhotographerCard() {
Column {
Text("Alfred Sisley", fontWeight = FontWeight.Bold)
// LocalContentAlpha is defining opacity level of its children
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Text("3 minutes ago", style = MaterialTheme.typography.body2)
}
}
}

@Preview
@Composable
fun PhotographerCardPreview() {
LayoutsCodelabTheme {
PhotographerCard()
}
}

最佳答案

这是 infix function 的示例:

Functions marked with the infix keyword can also be called using the infix notation (omitting the dot and the parentheses for the call).


infix 的存在可以看出 method's documentation 上的关键字.
所以该方法可以正常调用为 LocalContentAlpha.provides(ContentAlpha.medium) , 但中缀表示法允许删除那些额外的语法字符。

关于android - 这个 kotlin jetpack compose 代码示例中的 "provides"语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70424446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com