How to Use the "Also" And "Apply" Functions In Kotlin?

9 minutes read

In Kotlin, the "also" and "apply" functions are powerful tools that help in simplifying and improving the readability of code.


The "also" function is called on an object and allows you to perform some operations with that object while still returning the original object itself. The main purpose of "also" is to enable side effects like logging, debugging, or modifying properties of an object.


Here's an example to illustrate the usage of "also":

1
2
3
4
5
val person = Person().also {
    it.name = "John"
    it.age = 30
    println("Creating person: $it")
}


In this example, we create a Person object and modify its properties within the lambda expression passed to the "also" function. We also print out the person object for debugging purposes. The "also" function returns the original person object after the modifications.


The "apply" function is quite similar to "also" in terms of usage, but rather than returning the original object, it returns the receiver object itself. Its main purpose is to encapsulate a series of operations on an object in a concise and convenient way.


Here's an example using the "apply" function:

1
2
3
4
val person = Person().apply {
    name = "John"
    age = 30
}


In this example, we create a Person object and use "apply" to set its properties directly. The "apply" function returns the modified person object (this), which can be assigned to the person variable.


Overall, both "also" and "apply" functions provide a more expressive and concise way to work with objects, allowing you to perform operations and modifications in a more readable and efficient manner.

Best Kotlin Books to Read in 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Kotlin in Action

Rating is 4.9 out of 5

Kotlin in Action

3
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.8 out of 5

Kotlin Cookbook: A Problem-Focused Approach

4
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.7 out of 5

Head First Kotlin: A Brain-Friendly Guide

5
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.6 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

6
Effective Kotlin: Best Practices (Kotlin for Developers Book 5)

Rating is 4.5 out of 5

Effective Kotlin: Best Practices (Kotlin for Developers Book 5)

7
Java to Kotlin: A Refactoring Guidebook

Rating is 4.4 out of 5

Java to Kotlin: A Refactoring Guidebook

8
Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing

Rating is 4.3 out of 5

Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing


What are some alternative approaches to achieve the same functionality as the "also" function in Kotlin?

There are a few alternative approaches to achieve similar functionality as the "also" function in Kotlin. Some of them are:

  1. run: The "run" function is similar to "also" in that it executes a block of code and returns the result of the block. However, "run" is a scoping function, which means it can access the properties and methods of the object it is called on using the "this" keyword. This can be useful when you want to perform multiple operations on the object within the block.


Example:

1
2
3
4
5
6
7
8
val result = myObject.run {
    // perform operations on myObject
    doSomething()
    doAnotherThing()
    this.property = value
    this.method()
    return@run this.property
}


  1. apply: The "apply" function is another alternative to "also" that is frequently used for modifying properties of an object. It allows you to perform multiple property assignments on an object and returns the object itself. Similar to "also", it uses the "it" keyword to refer to the object.


Example:

1
2
3
4
5
val myObject = MyObject().apply {
    property1 = value1
    property2 = value2
    method()
}


  1. let: The "let" function is similar to "also" but works on the object it is called with as a parameter. It is often used for performing a chain of operations on a non-null object. The value of the block within "let" is treated as the return value.


Example:

1
2
3
4
5
val result = myObject?.let {
    doSomething(it)
    doAnotherThing(it)
    return@let result
}


These alternative approaches can offer flexibility in achieving similar functionality as the "also" function in Kotlin, depending on the specific use case and desired coding style.


Are there any limitations or caveats when using the "also" function in Kotlin?

The "also" function in Kotlin is quite versatile and can be used in various scenarios. However, there are a few limitations and caveats to keep in mind:

  1. Side Effects: The "also" function allows executing a block of code on an object and then returning the original object. It is primarily intended for performing side effects like logging, printing, or modifying mutable properties. While it is possible to modify the object in the lambda block, it is generally recommended to use the "let" function instead for such cases.
  2. Readability: The "also" function can lead to less readable code when used excessively or incorrectly. Using it too often can make the code harder to understand and maintain, especially if the operations performed inside the lambda block are non-trivial. It's important to strike a balance between readability and using the "also" function.
  3. Overusing immutability: Kotlin encourages the use of immutability, and "also" can be tempting to modify immutable objects. However, it's important to remember that modifying immutable objects might lead to confusion and unexpected behavior in code. It is generally recommended to use "also" with mutable objects only when necessary.


Overall, the "also" function is a powerful tool in Kotlin, but like any other function, it should be used judiciously and appropriately to maintain code readability and clarity.


How to use the "also" function with the "let" function in Kotlin?

In Kotlin, the let function is used to execute a block of code on a nullable object. The also function, on the other hand, is used to perform additional actions on an object and return the same object. To use the also function with the let function, you can chain them together like this:

1
2
3
4
5
6
7
8
9
val nullableValue: String? = "Hello, World!"

nullableValue?.let { value ->
    value.also {
        // Perform additional actions on the value
        // This block of code will receive the value as 'it'
        println("Value length: ${it.length}")
    }
}


In this example, the let function is used to access the non-null value inside the nullable object. Then, the also function is used to perform additional actions on that value, which is referred to as it inside the also block. You can replace it with a custom name if desired.


By chaining let and also functions together, you can both access the value safely and perform additional actions on it.

Twitter LinkedIn Telegram Whatsapp

Related Posts:

Working with the Kotlin Collections API allows you to efficiently manage and manipulate collections of data in your Kotlin code. Kotlin provides a rich set of built-in functions and operators that make it easy to perform common operations on lists, sets, and m...
Serializing and deserializing JSON in Kotlin involves converting JSON data into Kotlin objects and vice versa. Here's a step-by-step explanation of how to achieve this:Add the JSON serialization and deserialization library: Start by adding the necessary li...
To create a Kotlin class, follow these steps:Start by opening your Kotlin project in an integrated development environment (IDE) like IntelliJ IDEA or Android Studio. Create a new Kotlin file by right-clicking on the desired package or directory within your pr...