Kotlin apply() Function Tutorial 🎯

beginner
23 min

Kotlin apply() Function Tutorial 🎯

Welcome to our Kotlin tutorial on the apply() function! In this lesson, we'll explore how to use the apply() function in Kotlin, understand its purpose, and learn when to use it in your projects.

What is the apply() function in Kotlin? 📝

The apply() function is a built-in Kotlin extension function that returns the modified instance of an object after applying a series of changes defined within the block. It's a handy function that allows you to perform a set of operations on an object in a clean and concise way.

Why use the apply() function? 💡

The apply() function is particularly useful in the following scenarios:

  1. When you need to chain multiple method calls on the same object without having to repeatedly use the object reference.
  2. When you want to initialize an object and perform some setup tasks at the same time.
  3. When you want to avoid writing unnecessary lines of code, making your code cleaner and more readable.

How to use the apply() function? 🎯

Let's dive into a simple example to understand how to use the apply() function:

kotlin
class Person(var name: String, var age: Int) fun Person.applyGreeting() { println("Hello, I'm $name and I'm $age years old.") } fun main() { val john = Person("John", 25) john.applyGreeting() }

In the above example, we have defined a Person class with two properties: name and age. We also created an extension function called applyGreeting() that prints a greeting message using the name and age properties of the Person instance.

In the main() function, we create an instance of the Person class named john and call the applyGreeting() function on it. The applyGreeting() function is an extension function, so we can call it directly on the john object without needing to use the dot notation (.).

The output of the above example will be:

Hello, I'm John and I'm 25 years old.
Quick Quiz
Question 1 of 1

What is the purpose of the `applyGreeting()` function in the provided example?

Using the apply() function 🎯

Now that we've learned about the applyGreeting() function, let's see how we can use the apply() function with it:

kotlin
class Person(var name: String, var age: Int) fun Person.applyGreeting() { this.apply { println("Hello, I'm $name and I'm $age years old.") } } fun main() { val john = Person("John", 25) john.applyGreeting() }

In this example, we've made some modifications to the applyGreeting() function to use the apply() function instead of directly printing the greeting message. The apply() function is called within the applyGreeting() function, and it takes the this keyword as its argument, which refers to the current instance of the Person object.

The output of the above example will be the same as the previous example:

Hello, I'm John and I'm 25 years old.
Quick Quiz
Question 1 of 1

What does the `this` keyword refer to in the provided example?

Advanced usage of the apply() function 💡

You can also chain multiple apply() calls to perform multiple operations on the same object. Here's an example:

kotlin
class Person(var name: String, var age: Int, var job: String) fun Person.applyGreeting() { this.apply { println("Hello, I'm $name and I'm $age years old.") } } fun Person.applyJob() { this.apply { println("My job is $job.") } } fun main() { val john = Person("John", 25, "Developer") john.applyGreeting().applyJob() }

In this example, we've added a new property to the Person class called job. We've also created another extension function called applyJob() that prints the job property of the Person instance.

In the main() function, we create a Person instance named john and call both the applyGreeting() and applyJob() functions on it. The applyGreeting() function will be called first, and then the applyJob() function will be called on the modified john instance.

The output of the above example will be:

Hello, I'm John and I'm 25 years old. My job is Developer.
Quick Quiz
Question 1 of 1

What is the order of function calls in the provided example?

Conclusion 🎯

In this tutorial, we've explored the apply() function in Kotlin, learned its purpose, and seen how to use it in our projects. The apply() function is a valuable tool for chaining multiple method calls on the same object and performing initial setup tasks in a clean and concise way.

With that, you've now taken a step further in mastering Kotlin! Keep learning, practicing, and applying your newfound knowledge. 🎉

Happy coding! 💻