apply

inline fun <T> T.apply(block: T.() -> Unit): T(source)

Calls block with this value as its receiver and returns this value.

Use this function to configure a value: this inside the block is the original receiver, and the block result is discarded. Nullable receivers are passed through. The block runs once; its exceptions propagate and it supports non-local returns.

val text = StringBuilder().apply {
append("Hello")
append('!')
}.toString()

Receiver

value to configure and return.

Return

the original receiver, without copying it.

Parameters

block

action to run once with this receiver.

Type Parameters

T

type of the receiver, which may be nullable.

See also