run

inline fun <T, R> T.run(block: T.() -> R): R(source)

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

Inside the block, this refers to this value. Nullable receivers are passed through; a safe call skips the block when the receiver is null. The block runs once; its exceptions propagate and it supports non-local returns.

Receiver

value available as this in the block.

Return

the value produced by block.

Parameters

block

computation to run once.

Type Parameters

T

type of the receiver, which may be nullable.

R

type of the block result.

See also


inline fun <R> run(block: () -> R): R(source)

Calls block without a receiver and returns its result.

Use this overload to group statements into an expression with local variables. The block runs once; its exceptions propagate and it supports non-local returns.

val sum = run {
val left = 2
val right = 3
left + right
}

Return

the value produced by block.

Parameters

block

computation to run once.

Type Parameters

R

type of the block result.