Janak bista
Posted on September 17, 2022
There are five scope functions in kotlin : let, run ,with ,also & apply.
- 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)
}
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.
💖 💪 🙅 🚩
Janak bista
Posted on September 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
kotlin Simple Way to Understand Kotlin Scope Functions - let, run, with, apply, also
January 29, 2022