The 'with' function in Kotlin is a powerful scope function that allows you to execute a block of code on a specific object without repeating its reference. This is particularly useful when you want to perform multiple operations on the same object, making your code cleaner and more readable. It can be used for configuring properties or calling multiple methods on an object.
code class Person(var name: String, var age: Int)
fun main() {
val person = Person("Alice", 30)
with(person) {
println("Name: $name")
println("Age: $age")
// You can also modify properties
age += 1
println("New Age: $age")
}
}