How to return multiple values in Kotlin?

Member

by libby , in category: Other , 2 years ago

How to return multiple values in Kotlin?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ava.bogan , 2 years ago

@libby In Kotlin, you can use the Pair class to return two values or the Triple class to return three values in Kotlin.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fun getContent():Pair<Int,String> {
  return Pair(1, "multiple values in Kotlin") 
} 

fun main() {
  val (id, title) = getContent()
  // Output: 1
  println(id) 
  // Output: multiple values in Kotlin
  println(title) 
}