Using the 'let' Function for Conditional Execution

The 'let' function can be used not only for nullable handling but also for executing a block of code conditionally based on a specific condition. This is particularly useful when you want to perform certain actions only if an object meets specific criteria. By combining 'let' with a null check or other conditions, you can write cleaner and more expressive code.

val user: User? = getUser()

user?.let {
    if (it.isActive) {
        println("User is active: ${it.name}")
    } else {
        println("User is not active")
    }
}