Using the 'let' Function for Scoped Object Transformation

The 'let' function in Kotlin is not only useful for null safety but can also be employed for transforming objects within a scope. This technique is particularly beneficial when you want to operate on an object and return a transformed version of it without explicitly referencing the object each time.

data class User(val name: String, val age: Int)

fun main() {
    val user = User("Alice", 30)

    val userGreeting = user.let {
        "Hello, my name is ${it.name} and I am ${it.age} years old."
    }

    println(userGreeting)
}