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.
Top comments (0)