什么是“.()"?在科特林意味着什么? [英] What does ".()" mean in Kotlin?
问题描述
我看过一些示例,其中一个函数具有由ClassName.()提供的参数. 这似乎不是扩展功能,它是ClassName.Function()
I've seen examples where a function has an argument given by ClassName.() This doesn't seem to be an extension function, which is ClassName.Function()
一个例子是 Kotterknife :
private val View.viewFinder: View.(Int) -> View?
get() = { findViewById(it) }
我不太了解它的功能
drawer {
...
}
而不是使用括号括起来的参数.
rather than give it arguments surrounded by the parentheses.
在任何地方都有关于此的任何文档吗?
Is there any documentation on this anywhere?
推荐答案
在Kotlin中不接收任何内容且不返回任何内容的函数如下:
A function that takes in nothing and returns nothing in Kotlin looks like:
var function : () -> Unit
不同之处在于代码中的函数不返回任何内容,不接受任何内容,但在对象上调用.
While the difference is that function in your code returns nothing, takes in nothing but is invoked on an object.
例如,
class Builder (val multiplier: Int) {
fun invokeStuff(action: (Builder.() -> Unit)) {
this.action()
}
fun multiply(value: Int) : Int {
return value * multiplier
}
}
这里重要的一点是我们声明动作"类型的方式
The important bit here is the way we've declared the type of 'action'
action: (Builder.() -> Unit)
此函数不返回任何内容,不接收任何内容,但在类型为"Builder"的对象上调用.
This is a function that returns nothing, takes in nothing but is invoked on an object of type "Builder".
在此处引用更多 .
Refer more here.
这篇关于什么是“.()"?在科特林意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!