“.()”在 Kotlin 中是什么意思?

What does ".()" mean in Kotlin?

我见过函数具有由 ClassName.() 给出的参数的示例 这个好像不是扩展函数,就是ClassName.Function()

一个例子是Kotterknife:

private val View.viewFinder: View.(Int) -> View?
    get() = { findViewById(it) }

我不太了解它的功能,

MaterialDrawerKt

fun Activity.drawer(setup: DrawerBuilderKt.() -> Unit = {}): Drawer {
    val builder = DrawerBuilderKt(this)
    builder.setup()
    return builder.build()
}

代码允许您直接调用的地方

drawer {
    ...
}

而不是用括号括起来的参数。

有没有这方面的文档?

在 Kotlin 中不接受任何内容且 returns 不接受任何内容的函数看起来像:

var function : () -> Unit

区别在于代码中的函数不接收任何内容,returns 不接收任何内容,但是在对象上调用

例如,

class Builder (val multiplier: Int) {
    
    fun invokeStuff(action: (Builder.() -> Unit)) {
        this.action()
    }
    
    fun multiply(value: Int) : Int {
        return value * multiplier
    }
}

这里重要的一点是我们声明 action

类型的方式
action: (Builder.() -> Unit)

这是一个 returns 什么都不接受的函数,除了 Builder.

类型的对象上调用

这意味着当我们像这样使用这个构建器时

var builder = Builder(10)
builder.invokeStuff({
    var result = multiply(1)
    println(result)
})

this 的上下文已设置为构建器对象,我们可以调用在构建器中声明的函数。

Refer more here.

@Kris Roofe 的回答把事情说清楚了。让我添加更多内容。

fun Activity.drawer means that we are making an extension function name drawer in Activity class. That's the reason we can call drawer method directly from an Activity class or child of an Activity class.

有关扩展函数的更多信息 here

(setup: DrawerBuilderKt.() -> Unit = {}) In this statement we can see the power of kotlin higher order functions. Small intro of Higher order functions :- It is a function that takes functions as parameters, or returns a function. So here setup param is a function which return Nothing or Unit(Same as Void in java). DrawerBuilderKt.() means that the function can be invoked using object of DrawerBuilderKt class. = {} means that setup parameter is optional. So the function takes no parameters and return nothing.

更多关于高阶函数的信息here and here. More on optional parameter here

private val View.viewFinder: View.(Int) -> View? it store a function in a property. Here more info about the same. Rest of the things are same as explained above.

希望这会有所帮助。

这是个好问题。所以当你有这种说法时:T.()

这意味着在lamda中你将传入,"this"(当前对象)将是T类型。让我们看看它有多容易理解:

假设我们有一些 class 带有一个名为 myFun 的函数,它接收一个定义如下的 lambda:

 class MyObject {
        fun myFun(doSomething: MyObject.()->Unit) {
            doSomething()
        }

        fun doAnotherThing() {
            Timber.d("myapp", "doing another thing")
        }
    }

要调用此函数,我会这样做:

MyObject().myFun { doAnotherThing() }

看看它是如何知道将 MyObject() 引用用作 "this" 的。这实际上是在调用 this.doAnotherThing() ,这是刚刚创建的 Myobject() 实例。

也可以这样做:

MyObject().apply{myFun { doAnotherThing() }}  

有一种误解,认为T.() -> Y是(T.()) -> Y,实际上是T.(()->Y)。 我们知道 (X)->Y 是一个 lambda,所以 T.(X)->Y 是 T.

的扩展

如果没有参数,则为 T.() -> Y

有趣的是,我们可以用两种方式来称呼它。

import kotlinx.coroutines.*


open class MyClass(var name: String){
    open fun something(){println("myclass something")}
}


fun main() = runBlocking{
    val me = MyClass("Boll")
    val someMethod: MyClass.(Int) -> String = { n ->
        List(n){"X"}.joinToString(separator="", postfix=":${this.name}")
    }
    val some = me.someMethod(10)
    //val some = someMethod(me, 10)
    println(some)

    val anotherMehtod: MyClass.() -> String = { 
        "Y:"+this.name
    }
    //val another = me.anotherMehtod()
    val another = anotherMehtod(me) 
    println(another)
}