DEV Community

Janak  bista
Janak bista

Posted on

Scoped Function in Kotlin

There are five scope functions in kotlin : let, run ,with ,also & apply.

  1. let: It takes object it is invoked upon as the parameter & return the result of the lambda expression.let is used for checking nullable property. Syntax : var name : String? = "Hello" name?.let{println(it)}

For Example :

fun PerformOperation() {
val name:String = "Ram"
val person = Person.let {
return "Name: ${it.name}"
}
print(person)

}
Enter fullscreen mode Exit fullscreen mode

Output : Ram

2.also :
It does some additional processing on object it was involked
unlike let object. but returns original object instead of new
return data.

3.run :
It is similar to let function .It returns the last statement .It does not contain 'it' keyword.

4.with :
It is just like run operator .It refers context of the object.

Top comments (0)