Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <ClassName>.() mean in Kotlin?

Tags:

kotlin

Not sure what this means but I came across this syntax in the kotlin html codebase. What does SCRIPT.() mean?

https://github.com/Kotlin/kotlinx.html/blob/master/shared/src/main/kotlin/generated/gen-tag-unions.kt#L143

fun FlowOrPhrasingOrMetaDataContent.script(type : String? = null, src : String? = null, block : SCRIPT.() -> Unit = {}) : Unit = SCRIPT(attributesMapOf("type", type,"src", src), consumer).visit(block)

SCRIPT is a class - https://github.com/Kotlin/kotlinx.html/blob/master/shared/src/main/kotlin/generated/gen-tags-s.kt.

Or more generally, what does <ClassName>.() mean in Kotlin?

like image 746
sat Avatar asked Jan 13 '18 22:01

sat


People also ask

What is this () in Kotlin?

In Kotlin, the “this” keyword allows us to refer to the instance of a class whose function we happen to be running.

What is () -> unit in Kotlin?

Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

What does .() Mean in Kotlin?

it means that in the lamda you will be passing in, "this" (which is the current object) will be of type T.

How do you define a class in Kotlin?

To define a class in Kotlin, class keyword is used: class ClassName { // property // member function ... .. ... } Here, we defined a class named Lamp . The class has one property isOn (defined in same way as variable), and two member functions turnOn() and turnOff() .


Video Answer


1 Answers

Quick answer

block: SCRIPT.() -> Unit = {}

This represents a “function literal with receiver”. It’s a function parameter with a function type () -> Unit and SCRIPT as it’s receiver.

Function Literals/Lambda with Receiver

Kotlin supports the concept of “function literals with receivers”. It enables the access on visible methods and properties of a receiver of a lambda in its body without any specific qualifiers. This is very similar to extension functions in which it’s also possible to access visible members of the receiver object inside the extension.

A simple example, also one of the greatest functions in the Kotlin standard library, isapply:

public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

As you can see, such a function literal with receiver is taken as the argument block here. This block is simply executed and the receiver (which is an instance of T) is returned. In action this looks as follows:

val foo: Bar = Bar().apply {
    color = RED
    text = "Foo"
}

We instantiate an object of Bar and call apply on it. The instance of Bar becomes the “receiver”. The block, passed as an argument in {}(lambda expression) does not need to use additional qualifiers to access and modify the shown visible properties color and text.

The concept of lambdas with receiver is also the most important feature for writing DSLs with Kotlin.

like image 152
s1m0nw1 Avatar answered Oct 17 '22 17:10

s1m0nw1