Kotlin Scope Function Breakdown
Breakdown of Kotlin scope function and their uses.
Introduction:
Hey Android Floks! I am Rohit Jakhar, Native Android Developer having 2+ year experience.Welcome again in series on Kotlin which Named is Kotlin BreakDown. I will breakdown some Kotlin Topic in easy and fun way. Also i participate in Epic Hashnode Writeathon in #thw-mobile-apps
In this article we trying to breakdown Kotlin Scope Function Kotlin Scope Function are mostly used function and they can improve coding performance and less no of line in code. Kotlin provide 5 scope function: let, run, also, with, apply We will discuss more about them and understand each also understand where use which one.
this vs it scope
Before understanding scope function, we should clear about this and it object reference because some scope function provide this object reference and some are give it reference.
this: in simple word, this keyword mean current scope of body/function, we can avoid it for using property of current object or we can use it to make difference from global variable and local variable. And we can't rename it as we rename in it object reference. See Example:
fun main() {
var blogName:String? = "Hashnode Blog"
blogName.apply{
print(this)
}
}
we can't rename this keyword here.
it: In this Object Reference, we can rename it and also we should use it for accessing property of that object. See Example:
fun main() {
var blogName:String? = "Hashnode Blog"
blogName?.let{ blog ->
print(blog)
}
}
Cool! I think now we have some understanding of this and it object reference and if you noticed we use two scope function in above two example. Now time to understand scope function.
Scope Function
As the name suggests, scope functions re-scope the context of executing code. In simple word, we use scope function to re-scope an object as either this or it in the enclosing lambda.
The main purpose of scope function is write code more concise and readable. Also remember don't nested scope function mean don't use another scope function in scope function because it consider in bad code. Avoid it as you can.
let
let is most used scope function in Kotlin and the reason behind this is because let is useful for checking Nullable properties. In above example, we use ?.let this mean is that block execute when object is not null. Let see another best example.
fun main() {
val blogName:String? = "Hashnode Blog"
val nullBlog: String? = null
blogName?.let{ blog ->
print(blog)
}
nullBlog?.let{blog ->
print(blog)
}
}
Now guess the outout of above code. Its output is Hashnode Blog and reason you know. Right! Because in nullBlog we have null value and let block not execute.
also
also is interesting scope function because it return same object which mean the return data has always the same type. See Example:
fun main(){
var name: String? = "Rohit"
val result = name.also { it ->
println(it)
name = "Rohit Blog"
}
println(result)
}
Now guess what is output of above code. If you think second println print Rohit Blog so you wrong because also return same object without any change. So output of above code is Rohit Rohit.
with
with scope function is differenert from other scope function because it is not a extension function and also it take object as parameter and provide object reference this in the bracket. And with give return last line. with is useful when you need to performance some operation on object and need get back and remember it not check nullable.
See Example:
fun main(){
var name: String = "Rohit"
val result = with(name){
name = "Rohit Blog"
name
}
print(result)
}
As you see in code, we assign with() in result variable and when we print result we get "Rohit Blog" because we change name and return it. It is useful when we need perform more operation on same object.
run
run scope function is mixture of let and with function because it can check nullable and use as extension function but same time we can execute run scope function seprate so it can work as non extension function same as with function. And it return the last line same in with scope function.
See Example for more clearity.
fun main(){
var name: String? = "Rohit"
val result = name?.run {
println(this) // print object reference
"Rohit Blog" //Return new String
}
print(result) // print result
// use run without any object
run {
name = "Rohit Hashnode Blog"
println(name)
}
}
apply
apply is my favourit scope function because in Android Development i use it most of time after let function. apply is extension function run on object reference and return object reference on completion. It same as also but it give this object reference while also give it object reference. See Example:
fun main(){
val name: String? = "Rohit"
val result = name.apply {
println(this)
}
println(result)
}
Use
with, let, apply, also, run
- If the property is nullable, you can use let or run
- If the property is non-nullable, you can use with
- If you want to do something additional work with an object or result of some other function use apply or also.
Summary
this | it | |
self | apply | also |
result | run/with | let |
If you still have some doubt, ask in comment i will try to solve your doubt happily. If you like my explanation, tell me in comment section so i will make more blog on Kotlin Breakdown.