Open In App

Kotlin apply vs with

Last Updated : 14 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, we often need to modify properties or perform operations on an object. Instead of writing multiple lines of code to set values or perform actions on an object, Kotlin provides scope functions like apply and with to make this process concise, clear, and elegant. Both apply and with are useful for initializing or modifying objects, but they work slightly differently.

Kotlin :: apply

The apply function in Kotlin is an extension function that is invoked on an object, setting the scope to that object. Within the apply block, the keyword this refers to the object itself, allowing direct access to its properties and methods without needing the dot operator. After executing the block, apply returns the same object, making it particularly useful for chaining function calls or configuring objects. In addition to setting properties, the apply block can also contain other logic or operations related to the object.

Syntax:

inline fun <T> T.apply(block: T.() -> Unit): T {
block()
return this
}

where,

  • T: The type of the receiver object.
  • block: A lambda with receiver (T.() -> Unit) that operates on the object.

Example:

Kotlin
data class GFG(var name1: String, var name2: String, var name3: String)

fun main() {
    // instantiating object of class
    // apply function invoked to change the name3 value
    val obj = GFG("Geeks", "for", "hi").apply {
        name3 = "Geeks"
    }
    println(obj)
}

Output :

GFG(name1=Geeks, name2=for, name3=Geeks)

Here, the name3 property was modified inside the apply block, and the modified object was returned.

Kotlin :: with

The with function in Kotlin is not an extension function but a regular function. It takes an object as its argument, and within the block provided to with, the keyword this refers to that object. Unlike functions like apply, you don’t need to use the dot operator to access the object’s members inside the block. After executing the block, with returns the result of the last expression in the block, which means it doesn’t necessarily return the original object.

Syntax-

inline fun <T, R> with(receiver: T, block: T.() -> R): R {
return receiver.block()
}

where,

  • receiver: The object on which the block will operate.
  • block: The lambda to execute.
  • Returns: Whatever the last expression inside the block evaluates to.

Example:

Kotlin
data class GFG(var name1: String, var name2: String, var name3: String)

fun main() {
    val obj = GFG("Geeks", "for", "hi")
    // applying with function
    with(obj) {
        name1 = "Geeks"
        name3 = "Geeks"
    }
    println(obj)
}

Output :

GFG(name1=Geeks, name2=for, name3=Geeks)

Here, with was used to modify properties without the dot operator, and the object was modified directly.


Difference between apply and with

  • with runs without an object whereas apply needs one object to run
  • apply runs on the object reference, but with simply passes it as the argument


  • Article Tags :

    Similar Reads