android-studio - 为什么我在参数中添加多余的逗号时可以编译代码?
问题描述
我使用 Android Studio 北极狐,代码 A 来自项目。
并且代码 A 可以正确编译。我发现在参数列表中添加多余的逗号时Code B也可以编译,为什么?
代码 A
composable(Bills.name) {
BillsBody(bills = UserData.bills)
}
代码 B
composable(Bills.name, ) {
BillsBody(bills = UserData.bills)
}
相同
public fun NavGraphBuilder.composable(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
deepLinks: List<NavDeepLink> = emptyList(),
content: @Composable (NavBackStackEntry) -> Unit
) {
addDestination(
ComposeNavigator.Destination(provider[ComposeNavigator::class], content).apply {
this.route = route
arguments.forEach { (argumentName, argument) ->
addArgument(argumentName, argument)
}
deepLinks.forEach { deepLink ->
addDeepLink(deepLink)
}
}
)
}
解决方案
这称为尾随逗号,是在 Kotlin版本 1.4中添加的。
尾随逗号背后的主要原因之一 - 使用它们可以减少列表长度更改时需要更新的行数。使提交更干净。
推荐阅读
- java - Spring mvc Web应用程序无法按要求工作
- java - 如何在 pom 中使用命令行参数?
- angular - 属性 .... 没有初始化程序,也没有在构造函数中明确分配
- javascript - Firebase auth/user-token-expired:抛出的错误在哪里?(JS)
- python - 堆积面积图 Seaborn 唯一值 X 轴
- google-apps-script - 脚本保持身份验证多长时间?
- sql - athena 列包含列表类型数据
- python - pipenv 安装包在没有 pip install 的情况下发生`ModuleNotFoundError`
- python - 在模块内巧妙地实例化类
- sql - 选择列表中的“X”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中