What does ". ()" Mean in Kotlin?

I've seen examples where a function has an argument given by ClassName. () It seems to be not an extension function, but a ClassName.Function ()

An example is Kotterknife :

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

      

Which I don't quite know the function,

and MaterialDrawerKt

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

      

Where the code allows you to call directly

drawer {
    ...
}

      

instead of giving the arguments in parentheses.

Is there any documentation on this issue somewhere?

+15


source to share


3 answers


A function that accepts nothing and returns nothing in Kotlin looks like this:

var function : () -> Unit

      

Although the difference is that the function in your code does not return anything, it receives nothing but is called on the object.

For example,

class Builder (val multiplier: Int) {

    fun invokeStuff(action: (Builder.() -> Unit)) {
        this.action()
    }

    fun multiply(value: Int) : Int {
        return value * multiplier
    }
}

      



The important thing here is how we declared the "action" type

action: (Builder.() -> Unit)

      

This is a function that returns nothing, accepts nothing, but is called for an object of type "Builder".

Refer here .

+11


source


@Kris Roofe's answer clarifies things. Let me add more to this.

fun Activity.drawer means we create the name of the extension function drawer

in the class Activity

. This is why we can call the drawer method directly from the Activity class or a descendant of the Activity class.

Read more about extension functions here .

(setup: DrawerBuilderKt. () → Unit = {}) In this statement we see the power of higher order Kotlin functions. Small introduction of Higher Order Functions: - It is a function that takes functions as parameters, or returns a function.

So, here the setting parameter is a function that does not return anything or Unit (same as Void in Java). DrawerBuilderKt. () Means that the function can be called using an object of the class DrawerBuilderKt

. = {} means the setup parameter is optional. Thus, the function takes no parameters and returns nothing.



More about higher order functions here and here . Read more about the optional parameter here .

private val View.viewFinder: View. (Int) → View? store the function in a property. Here's more information on the same. The rest of the things are the same as described above.

Hope this helps.

+3


source


That's a good question. so when you have a statement like this:T.()

this means that in the lambda that you pass, "this" (which is the current object) will be of type T. Let's see how easy it is to understand it:

Let's say we have some class with myFun function that accepts a lambda defined like this:

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

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

      

to call this function I would do this:

MyObject().myFun { doAnotherThing() }

      

see how he could use the MyObject () reference as "this". this actually calls this.doAnotherThing () where it is the newly created instance of Myobject ().

could also do this:

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

      

+3


source







All Articles