0
0
Kotlinprogramming~10 mins

With function behavior and use cases in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - With function behavior and use cases
Call with(obj) { block }
Inside block: 'this' refers to obj
Execute block using obj's members
Return block result
End
The with function takes an object and a block of code. Inside the block, 'this' means the object, so you can call its methods or properties directly. It returns the block's result.
Execution Sample
Kotlin
val person = Person("Anna", 25)
val result = with(person) {
    "Name: $name, Age: $age"
}
println(result)
Creates a Person object and uses with to access its properties directly, returning a formatted string.
Execution Table
StepActionEvaluationResult
1Create person objectPerson(name="Anna", age=25)person = Person("Anna", 25)
2Call with(person) { ... }Enter block with 'this' = personContext set to person
3Evaluate block: "Name: $name, Age: $age"Substitute name and age from person"Name: Anna, Age: 25"
4Return block resultBlock returns stringresult = "Name: Anna, Age: 25"
5Print resultOutput to consoleName: Anna, Age: 25
💡 Block executed fully, with returned string assigned to result and printed.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
personnullPerson(name="Anna", age=25)Person(name="Anna", age=25)Person(name="Anna", age=25)
resultnullnull"Name: Anna, Age: 25""Name: Anna, Age: 25"
Key Moments - 3 Insights
Why can we access 'name' and 'age' without prefix inside the with block?
Inside the with block, 'this' refers to the object passed (person), so its properties can be accessed directly without prefix, as shown in step 3 of the execution_table.
What does the with function return?
The with function returns the last expression inside its block. Here, it returns the formatted string, as shown in step 4 of the execution_table.
Can we use with to modify the object properties?
Yes, inside the with block you can call methods or set properties on the object because 'this' refers to it. This is implied by the context set in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 4?
Anull
BPerson object
C"Name: Anna, Age: 25"
D"Anna, 25"
💡 Hint
Check the 'Result' column in row for step 4 in execution_table.
At which step does the with function set 'this' to the person object?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing entering the block with 'this' set.
If we changed the block to modify 'age' by adding 1, what would happen to 'person.age' after with?
AIt stays the same
BIt increases by 1
CIt becomes null
DIt throws an error
💡 Hint
Recall that inside with, 'this' is the object, so modifying properties affects the original object.
Concept Snapshot
with(obj) { block } runs block with 'this' as obj.
Inside block, access obj's members directly.
Returns last expression of block.
Useful for grouping calls on one object.
Simplifies code by avoiding repeated obj references.
Full Transcript
The with function in Kotlin takes an object and a block of code. When you call with(obj) { block }, inside the block 'this' means the object, so you can use its properties and methods directly without repeating the object name. The function runs the block and returns the last expression's value. For example, if you have a person object with name and age, you can write with(person) { "Name: $name, Age: $age" } to get a formatted string. This makes code cleaner and easier to read. You can also modify the object inside the block because 'this' refers to it. The execution steps show creating the object, entering the block with 'this' set, evaluating the block, returning the result, and printing it. Variables person and result change as expected. Common confusions include why you can access properties without prefix, what with returns, and if you can modify the object inside. The quiz checks understanding of these points.