kotlin_interview_tips
Mohamad Abuzaid 1 year ago
mohamad-abuzaid #kotlin-tips

Kotlin Scope Functions

What are the "Scope Functions"? What are the difference between them?


Scope functions in Kotlin are a set of functions that allow you to execute a block of code within the context of an object and return a result. They provide a convenient way to perform operations on an object within a scope and can simplify the code structure. The scope functions in Kotlin are: let, run, with, apply, and also. Each function has a specific use case and behavior, but they all help to make the code more readable and concise.

----------------------

[1] let


let allows you to execute a block of code on an object and return the result.

The basic syntax of the let function is as follows:

val name = "John Doe"
val upperCasedName = name.let {
    it.toUpperCase()
}
println(upperCasedName) // Output: JOHN DOE


In this example, name is the object on which the code block is executed and the result of the code block is returned.

Here, the let function is used to convert the name string to uppercase. The it keyword is used to refer to the object being passed to the let function. The result of the code block, which is the uppercase string, is stored in the upperCaseName variable and then printed to the console.

Another useful way to use let is to check nullability of the object before executing the code.

var name: String? = null

val upperCasedName = name?.let {
    it.toUpperCase()
}
println(upperCasedName) // Output: JOHN DOE


In this example, we will check if name object is null first before executing the block. If so, the expression returns null and the block is not executed.

------------------------

[2] run


run is similar to let but it does not require you to specify a parameter for the object, as it is passed implicitly.

The basic syntax of the run function is as follows:

val name = "John Doe"
val upperCasedName = name.run {
    toUpperCase()
}
println(upperCasedName) // Output: JOHN DOE


In this example, name is the object on which the code block is executed, and the result of the code block is returned.

---------------------

[3] with


with is similar to run but it requires you to specify the object as an argument.

The basic syntax of the with function is as follows:

val name = "John Doe"
val upperCasedName = with(name) {
    toUpperCase()
}
println(upperCasedName) // Output: JOHN DOE


In this example, name is the object on which the code block is executed, and the result of the code block is returned.

-------------------------

[4] apply


The main difference between apply and the other scope functions is that apply is designed to modify the object itself, rather than simply returning a result.

The basic syntax of the apply function is as follows:

val stringBuilder = StringBuilder().apply {
    append("Hello")
    append(" ")
    append("World")
}
println(stringBuilder) // Output: Hello World


In this example, name is the object on which the code block is executed, and the original object is returned after the code block has been executed.

Here, the apply function is used to build a string by appending multiple strings to a StringBuilder object. The StringBuilder object is passed to the apply function and can be modified inside the code block. The original object is returned after the code block has been executed, and the resulting string is printed to the console.

--------------------

[5] also


also is similar to apply, but it does not allow you to modify the object inside the code block. Instead, it is typically used to perform some operation on the object, such as logging or debugging, without changing its state.

The basic syntax of the also function is as follows:

val name = "John Doe"
val length = name.also {
    println("The length of the string is: ${it.length}")
}.length
println(length) // Output: The length of the string is: 8
               //        8


In this example, name is the object on which the code block is executed, and the original object is returned after the code block has been executed.

Here, the also function is used to log the length of the name string. The name string is passed to the also function and can be referred to inside the code block using the it keyword. The original object is returned after the code block has been executed, and its length is stored in the length variable and then printed to the console.

1
687
Kotlin Delegated Properties

Kotlin Delegated Properties

1675112374.jpg
Mohamad Abuzaid
1 year ago
HashMap Operations Complexity O(N)

HashMap Operations Complexity O(N)

1675112374.jpg
Mohamad Abuzaid
1 year ago
Android Memory Leaks

Android Memory Leaks

1675112374.jpg
Mohamad Abuzaid
1 year ago
Design Patterns - [1] Creational

Design Patterns - [1] Creational

1675112374.jpg
Mohamad Abuzaid
1 year ago
How to create custom annotations in Kotlin

How to create custom annotations in Kotlin

1675112374.jpg
Mohamad Abuzaid
6 months ago