Tuple
Code
AI Gemini
Tuples are not directly supported in Kotlin. However, their behavior can be mimicked using data classes, built-in types like Pair
and Triple
, collection types, or custom types with componentN
functions. These alternatives enable returning multiple values from a function, similar to tuples in other languages.
fun returnPair(): Pair<Int, String> {
return Pair(1, "hello")
}
val (number, text) = returnPair()
println("Number: $number, Text: $text")
Data classes offer a more structured approach, especially when dealing with more than two values or when named properties are desired:
data class Person(val name: String, val age: Int, val city: String)
fun returnPerson(): Person {
return Person("Alice", 30, "New York")
}
val (name, age, city) = returnPerson()
println("Name: $name, Age: $age, City: $city")