Scope functions in Kotlin
Kotlin is quite simplified language and it provides various inbuilt functions to ease the life of developers.
Scoped functions accepts the blocked of code within context of object and when you call these functions it temporarily form the scopes.
There are total 5 types of scope functions.
- let
- run
- with
- apply
- also
We will study each function one by one and this blog will be different and more like cheet sheet to remember the usage of each function.
Name : let
Lambda param inside code block : it
Return : last element of code block
Example:
data class User(var name: String, var email: String)
val user: User? = User(name = "Smith", email = "abc@g.co")
user?.name = "John"
user?.email = "john@g.co"
In above example, as user is nullable we have to check everytime by adding null safety check(?) and use the object. There is better way handle this.
var x = user?.let {
print(it.name)
print(it.email)
it.name
}
here we use the let to check the null safety, now inside the lambda function non null object is not possible, and we don’t have to worry about null pointer exception.
Here let will return last element of the code block which is string as we are returning name of user.
Name : run
Lambda param inside code block : this
Return : last element of code block
Example:
user?.run {
print(name)
}
Usage : It is just like let with only 1 change, in lambda function you will have the context of object instead of it. Run also returns last element of lambda function. Can be use for null safety check
Name: with
Lambda param inside code block: this
Return : last element of code block
Example:
with(user){
print(name)
}
We have to pass the object to with function which we can use in lambda block directly.
Can’t be use for null safety.
Returns the last element of lambda function. In our example it will return unit.
Name: apply
Lambda param: this (Same object context)
Return : Same object is retured
Example:
data class User(var name: String = "", var email: String = "")
fun main() {
val user = User()
val user1 = user.apply {
name = "Smith"
email = "xyz@g.co"
}
println("user:"+user1)
}
//output user:User(name=Smith, email=xyz@g.co)
Usage: We can use this function when we want to set value . In lambda function we get context of same object. We can directly access the elements.
Name : also
Lambda param inside code block : it
Return : Same object is retured
data class User(var name: String = "", var email: String = "", var age: Int = 0)
fun main() {
val user1: User = User(name = "Smith", email = "abc@g.co")
val b = user1.also {
it.age = 10
}
print(b)
}
//User(name=John, email=john@g.co, age=10)
This function is combination of let and apply,
In return we will get the same object just like apply, while in lambda function we will get it object to do any operations.
So to remember this let, run and with returns the last element of lambda function while apply and also returns same object.
let and also uses it in lambda function while run, with and apply deals with actual context of object.
Hope everyone is clear now with this concepts. Let’s cover some amazing concepts in next blog.