Sobes.tech
Junior — Middle

Explain what destructuring a function is and how it is used in programming.

sobes.tech AI

Answer from AI

Function destructuring is a way of unpacking values from an object or array directly into function parameters. In programming, it allows for convenient extraction of necessary data without unnecessary property accesses.

For example, in Kotlin, destructuring can be used in functions that return multiple values:

// Function returns a pair of values
fun getUser(): Pair<String, Int> = "Alice" to 30

// Destructuring upon call
val (name, age) = getUser()
println("Name: $name, Age: $age")

Thus, destructuring simplifies working with complex data and makes the code more readable.