Kotlin

To share this page click on the buttons below;

Scope functions

Scope functions are run, with, apply, let and also.

Scope functions are used to execute a code within the context of an object. Functions scope require a lambda expression to define the code to execute and provides to the lambda expression the object they are applied to in some form.

<object>.<scope function> {
   <lambda expression>
   // 
}

object is made available as it or this depending on the <scope function> used.

This

this is the way the scope functions run, with, apply made available the context object to the lambda expression. With this the object is available in lambda context and it is the preferred solution for lambdas that mainly operate on object members.

It

it is the way the scope functions let and also made available the context object to the lambda expression. With it the object is not available in the lambda so that it is the preferred way when the object is the argument for function call.

Return value

  • applyand also return the context object
  • let, run, and with return the lambda result.

To share this page click on the buttons below;