📖 Scenario: Imagine you are organizing a small party and you want to keep track of guests and their favorite drinks. You will use Kotlin's scope functions to handle this data easily.
🎯 Goal: You will learn the difference between this and it receivers in Kotlin by using apply and let functions to manage guest information.
📋 What You'll Learn
Create a data class called Guest with properties name and favoriteDrink
Create an instance of Guest with specific values
Use apply to update the guest's favorite drink using this receiver
Use let to print a message using it receiver
💡 Why This Matters
🌍 Real World
Using <code>this</code> and <code>it</code> receivers helps Kotlin developers write clean and concise code when working with objects, especially in configuration and data manipulation tasks.
💼 Career
Understanding scope functions and their receivers is important for Kotlin developers working on Android apps or backend services, as it improves code readability and reduces errors.
Progress0 / 4 steps
1
Create the Guest data class and an instance
Create a data class called Guest with properties name of type String and favoriteDrink of type String. Then create a variable called guest and assign it an instance of Guest with name set to "Alice" and favoriteDrink set to "Water".
Kotlin
Hint
Use data class Guest(var name: String, var favoriteDrink: String) to define the class.
Create guest with Guest("Alice", "Water").
2
Use apply with this receiver to update favoriteDrink
Use the apply function on the guest variable to change the favoriteDrink property to "Lemonade". Inside apply, use this.favoriteDrink to set the new value.
Kotlin
Hint
Inside apply, this refers to the guest object. Use this.favoriteDrink = "Lemonade".
3
Use let with it receiver to print a message
Use the let function on the guest variable to print the message "Alice's favorite drink is Lemonade". Inside let, use it.name and it.favoriteDrink to build the message.
Kotlin
Hint
Inside let, it refers to the guest object. Use println("${it.name}'s favorite drink is ${it.favoriteDrink}").
4
Print the updated favorite drink using both receivers
Print two lines: first, use apply on guest to set favoriteDrink to "Orange Juice" using this.favoriteDrink. Then use let on guest to print "Alice now likes Orange Juice" using it.name and it.favoriteDrink.
Kotlin
Hint
First use apply to update the drink, then let to print the message.
Practice
(1/5)
1. In Kotlin, what does this refer to inside a class method?
easy
A. The current instance of the class
B. The first parameter of a lambda
C. A global variable
D. A static method
Solution
Step 1: Understand this in class context
Inside a class method, this refers to the current object instance that called the method.
Step 2: Differentiate from lambda parameter
this is not a lambda parameter; that role is usually played by it in single-parameter lambdas.
Final Answer:
The current instance of the class -> Option A
Quick Check:
this in class = current object [OK]
Hint: Remember: this means current object in classes [OK]
Common Mistakes:
Confusing this with lambda parameter it
Thinking this is a global variable
Assuming this refers to static context
2. Which of the following is the correct way to refer to the implicit lambda parameter in Kotlin when there is only one parameter?
easy
A. this
B. it
C. self
D. lambda
Solution
Step 1: Identify default lambda parameter
In Kotlin, when a lambda has a single parameter and no explicit name, it is referred to as it.
Step 2: Confirm this is not used here
this refers to the receiver object, not the lambda parameter.
Final Answer:
it -> Option B
Quick Check:
Single lambda parameter = it [OK]
Hint: Single lambda parameter? Use it [OK]
Common Mistakes:
Using this instead of it in lambdas
Trying to name the parameter without declaration
Assuming self is valid in Kotlin
3. What will be the output of the following Kotlin code?
class Box(val value: Int) {
fun printValue() {
listOf(1).forEach {
println(this.value + it)
}
}
}
fun main() {
Box(5).printValue()
}
medium
A. 6
B. 1
C. 5
D. Compilation error
Solution
Step 1: Understand this inside lambda with receiver
Inside printValue, this refers to the Box instance, so this.value is 5.
Step 2: Calculate the sum inside lambda
The lambda parameter it is 1 from the list. So, this.value + it = 5 + 1 = 6.
Final Answer:
6 -> Option A
Quick Check:
this.value + it = 6 [OK]
Hint: Inside lambda, this is outer object, it is lambda param [OK]
Common Mistakes:
Thinking this refers to lambda receiver
Confusing it with this
Expecting compilation error due to this usage
4. Identify the error in the following Kotlin code snippet:
fun main() {
val numbers = listOf(1, 2, 3)
numbers.forEach {
println(this + it)
}
}
medium
A. Missing lambda parameter name
B. Incorrect list declaration
C. Using this inside lambda without receiver causes error
D. No error, code runs fine
Solution
Step 1: Check this usage inside lambda
In this lambda, there is no receiver object, so this is not defined and causes an error.
Step 2: Confirm lambda parameter usage
it is valid as the lambda parameter, but this cannot be used without a receiver context.
Final Answer:
Using this inside lambda without receiver causes error -> Option C
Quick Check:
this needs receiver, else error [OK]
Hint: No receiver? Don't use this inside lambda [OK]
Common Mistakes:
Assuming this always refers to lambda parameter
Ignoring that this requires receiver context
Thinking code runs without error
5. Given the following Kotlin code, what will be printed?
class Calculator {
fun calculate() {
val numbers = listOf(10, 20)
numbers.run {
println(this.sum())
numbers.forEach {
println(it + this.sum())
}
}
}
}
fun main() {
Calculator().calculate()
}
hard
A. 10\n20\n30
B. Compilation error due to ambiguous this
C. 30\n30\n30
D. 30\n40\n50
Solution
Step 1: Understand this inside run block
Inside run, this refers to the numbers list. So, this.sum() is 30.
Step 2: Calculate outputs inside forEach
For each it in numbers (10 and 20), print it + this.sum(): 10+30=40 and 20+30=50.
Step 3: List all printed lines
First print: 30 Then prints: 40 and 50
Final Answer:
30
40
50 -> Option D
Quick Check:
this in run = list, sum=30 [OK]
Hint: Inside run, this is receiver object [OK]
Common Mistakes:
Confusing this inside nested lambdas
Assuming this refers to Calculator instance
Expecting compilation error due to this
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.