This simple trick to return multiple values from Kotlin Functions
Most programming languages, including Kotlin, only allow returning one value from function but if we need multiple return type from same function then how we can do it.
So for this solution, We will try to fix this problem and will be going to learn how to return multiple values from a Kotlin function.
1. Using Data class
The idiomatic way of returning multiple values from a function is to define your data class and return its instance from the function. Then you can unpack values using destructuring declaration inside the caller function.
data class Person(var name: String, var age: Int, var gender: Char)fun fetchPersonDetails(): Person { val name = "Riya" val age = 18 val gender = 'M'return Person(name, age, gender)}fun main() {val (name, age, gender) =
fetchPersonDetails() // destructuring declarationprintln("($name, $age, $gender)") // (Riya, 18, M)}
2. Using Pair
Kotlin has generic Pair types that can return two values from the function.
Note that it’s preferred to have your data named properly, and we should use a data class to return more values.
fun fetchPersonDetails(): Pair<String, Int> {
val name = "Riya"
val age = 18
return Pair(name, age)
}fun main() { val (name, age) = fetchPersonDetails()
println("($name, $age)") // (Riya, 18)}
3. Using Triple
Kotlin has generic Triple types that can return three values from the function.
Note that it’s preferred to have your data named properly, and we should use a data class to return more values.
fun fetchPersonDetails(): Triple<String, Int, Char> {val name = "Riya"val age = 18val gender = 'M'return Triple(name, age, gender)}fun main() {val (name, age, gender) = fetchPersonDetails()
println("($name, $age, $gender)") // (Riya, 18, M)}
4. Using return an array
Another approach is to accumulate your values inside an array and return it from the function. Note that this doesn’t offer type safety for an object array or pass field information to the caller.
fun fetchPersonDetails(): Array<Any> { val name = "Riya" val age = 18 val gender = 'M'return arrayOf(name, age, gender)}fun main() { val (name, age, gender) = fetchPersonDetails() println("($name, $age, $gender)") // (Riya, 18, M)}
5. Using custom componentN Functions
Another approach is to accumulate your values using ComponentN functions as extension functions to take advantage of destructuring declarations.
fun main(){
val classA=TestClass()
val (publicKey, privateKey) = classA.getRsaKeyPair()
println(""+publicKey)
println(""+privateKey)
}
private operator fun KeyPair.component2(): String {
return "Srivastava"
}
private operator fun KeyPair.component1(): String {
return "Abhishek"
}
class TestClass{
operator fun KeyPair.component1(): String = public.encoded.toString()
operator fun KeyPair.component2(): String = private.encoded.toString()
fun getRsaKeyPair(): KeyPair = KeyPairGenerator
.getInstance("RSA")
.genKeyPair()
}
And when code will run then you can see the output like below
Thanks for reading.