let{} and run{} Scope fun
Praveen Selvakumar
Android developer| MVVM| Flutter | Dart | kotlin | Java | Immediate Joiner
Good morning fellow developers ,
Let's grab a cup a coffee and recall a functional feature and type of functions as a feature , named as Scope functions .
This Scope function are the functions which uses temporary scope of any object to manage and perform any operation using the context of that object as a reference ,these are made mainly to resolve the daily hardships we are facing like readability, maintainability , to make code more concise(less vocabulary) , efficient object handling etc....
As an old , there are five type's of scope functions mainly ,
All the scope functions which is mentioned above have different purpose and slightly different use case ,even they look somewhat similar though ,
let{} scope function
var name = "Praveen Selvakumar";
name.let{
println(it)
}
//OR
name.let { myName ->
println(myName)
}
Use Case
领英推荐
object?.let{
//perform any operations
}
above example will be promising if you are handling with any nullable objects.
run{} scope function
//WITHOUT OBJECT REFERENCE
run {
println(name)
}
//OR
//WITH OBJECT REFERENCE
name.run {
println(this)
}
Use Case
//CONSIDER OBJECT IS A STRING
object.run {
print(object)
}
object.run{
this = "another value"
}
object.run{
this.plus(" Selva kumar")
}
//etc...
We have even more example using let and scope let's discuss it more detail on upcoming article.
Thank you.