Passing a lambda to the last parameter
在Kotlin中,有一个约定,如果
val product = items.fold(1) { acc, e -> acc * e }
What is the purpose of this syntax?
Passing a lambda to the last parameter
在Kotlin中,有一个约定,如果
val product = items.fold(1) { acc, e -> acc * e }
What is the purpose of this syntax?
这种语法为Kotlin提供了强大的DSL功能,它使函数看起来像语言 struct .例如:
with(car) {
startUp()
goToDestination()
}
这里with
看起来像是语言构造,而它是一个简单的函数,接收lambda作为最后一个参数.
And this leads to such elegant things like Kotlin HTML DSL