首页 > 解决方案 > 为什么我在参数中添加多余的逗号时可以编译代码?

问题描述

我使用 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)
            }
        }
    )
}

标签: android-studio

解决方案


这称为尾随逗号,是在 Kotlin版本 1.4中添加的。

尾随逗号背后的主要原因之一 - 使用它们可以减少列表长度更改时需要更新的行数。使提交更干净。


推荐阅读