Kotlin – Scope Function

What are Scope functions? 

Kotlin provides functions known as Scope functions in the standard library to execute a block of code within the context of an object. 

It offers a temporary scope when you call the function on the object with a lambda expression. In Scope, the function can access the object without its name.

There are five scope functions we'll cover in this blog. 

  1. Let
  2. Run
  3. With
  4. Apply
  5. Also

Scope represents the lambda expression to create a temporary scope. Hence, It's called Scope functions. It has Object access to its functions without its name.

Example: Without using scope function.

  //Create class of student
  class Student()
   {
    lateinit var name: String
    lateinit var subject: String
   }
  fun main()
   {
    // without using scope function
    val std = Student()
    // initializing members of the class
    std.name = "Candidroot Solutions"
    println(std.name)
   } 

Output


  CandidRoot Solutions


Example: With using the scope function

  //Create a class of student
   class Student() {
    lateinit var name: String
    lateinit var subject: String
   }
   fun main() {
  // without using scope function
    Val std = Student().apply{
   name = "Candidroot Solutions"
   subject= "software "
   }
   println(std.name)
   }

Output

CandidRoot Solutions

Application of using scope function

Scope functions make code clear, readable, and concise in application.

As we have discussed above, there are five types of scope functions

  1. let
  2. run
  3. with
  4. apply
  5. Also

Each of their functions is similar but has minor differences. It is very useful in applications.

1. Let function

           Context object: it

           Return value: lambda result 

Let function is used to provide null safety calls. So I used a safe call operator(?). It executes the block only with the non-null value.

 Example: 

fun main()
{
   // with value as null
    var a: Int? = null
   // using let function
    a?.let {
    // not execute as a is null
    print(it)
   }
   // re-initializing value of a to 2
    a = 565 //null value not accept
   a?.let {
   print(a)
}
}

Output

565

2. Apply  function

     Context object: this

     Return value: context  result 

Apply these to the object it can be used to operate on members.

Example:

   //Create class for student
    class Student() {
    lateinit var name: String
    lateinit var subject: String
    }
    fun main() {
   // without using scope function
    val std = Student().apply{
    name = "Candidroot Solutions"
    subject= "software "
    }
    println(std.name)
    }

Output

CandidRoot Solutions

3. With  function

    Context object: this

    Return value: lambda  result 

Use a calling function on a context object without providing the lambda result.

Example: 

   class Student() {
    lateinit var name: String
    lateinit var subject: String
    }
    fun main() {
    val std = Student().apply {
    name = "Candidroot solutions"
    subject = "Software"
    }
    // with function
    with(std) {
    println(" $name ")
    }
    }

4. run  function

     Context object: this

     Return value: lambda  result 

The run function is used as a combination of let and with functions.

 Example: 

   class Student() {
    lateinit var name: String
    lateinit var subject: String
    }
    fun main(args: Array) {
    println("Student Name : ")
    var student: Student? = null
    // Student is non-null
    student?.run {
    print(name)
    }
    print("Student Name : ")
    student = Student().apply {
    name = "Candidroot solutions"
    subject = "Software"
    }
    // company is non-null
    student?.run {
    print(name)
    }
}

Output

 Student Name:
 Student Name: CandidRoot Solutions

5. also  function

     Context object: this

     Return value: context  result 

It is used where we have to perform additional operations.

 Example: 

   fun main() {
   // initialized
    val list = mutableListOf(7, 2, 3)
   // multiple operations on this list
    list.also {
    it.add(4)
    it.remove(2)
    }
    println(list)
    }

Output

[7, 3, 4]

In conclusion, you have learned the process in the above steps for how Kotlin Scope works. As discussed above, Kotlin scope functions improve readability and make the code clear and concise.


365Bloggy May 8, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive