0
0
Kotlinprogramming~10 mins

Nullable receiver extensions in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nullable receiver extensions
Call extension on nullable receiver
Check if receiver is null?
YesReturn null or default
No
Execute extension function body
Return result
When calling an extension on a nullable receiver, Kotlin checks if the receiver is null before running the extension code. If null, it can return null or a default value.
Execution Sample
Kotlin
fun String?.shout() = this?.uppercase()

val name: String? = null
println(name.shout())
Defines an extension on a nullable String that returns uppercase or null, then calls it on a null value.
Execution Table
StepReceiver (this)Check null?ActionResult
1nullIs null?YesReturn null
2nullExtension call ends-null printed
💡 Receiver is null, so extension returns null without running uppercase()
Variable Tracker
VariableStartAfter callFinal
namenullnullnull
this (receiver)nullnullnull
resultundefinednullnull
Key Moments - 2 Insights
Why does the extension return null instead of throwing an error when receiver is null?
Because the extension uses safe call (this?.uppercase()), it checks for null and returns null early as shown in execution_table step 1.
What happens if we call the extension on a non-null string?
The extension runs uppercase() on the string and returns the uppercase version, as the null check in execution_table step 1 would be No.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result when the receiver is null?
Anull
BEmpty string
CThrows NullPointerException
DOriginal string
💡 Hint
See execution_table row 1 where receiver is null and result is null
At which step does the extension decide not to run uppercase()?
ABefore step 1
BStep 2
CStep 1
DAfter step 2
💡 Hint
Check execution_table step 1 where null check returns Yes and skips uppercase()
If the receiver was "hello", what would the result be?
A"hello"
B"HELLO"
Cnull
DThrows error
💡 Hint
Based on variable_tracker and concept_flow, non-null receiver runs uppercase()
Concept Snapshot
Nullable receiver extensions:
- Define with receiver type nullable (e.g., String?)
- Use safe calls inside extension (this?.method())
- If receiver is null, extension returns null or default
- Prevents NullPointerException on null receivers
- Allows calling extensions safely on nullable objects
Full Transcript
This visual trace shows how Kotlin handles nullable receiver extensions. When you call an extension function on a nullable receiver, Kotlin first checks if the receiver is null. If it is null, the extension returns null or a default value without running the function body. For example, the extension 'shout' on String? uses a safe call 'this?.uppercase()'. When called on null, it returns null immediately. The execution table shows step 1 checking null and returning null, and step 2 ending the call. The variable tracker shows the receiver and result values. This prevents errors and lets you safely call extensions on nullable objects.