How to use tuple alternatives to return multiple values from a Kotlin function

In computer programming, a Tuple is an ordered sequence of values that you can define arbitrarily.

A Tuple data type is commonly used to return multiple values from a function.

For example in Python, a Tuple can be defined as follows:

myTuple = ("apple", 2, False)

While it may look like an Array or a List, a Tuple is different because it can contain a sequence of values with different types.

As in the above example, the myTuple contains a string, an integer, and a boolean value.

In Kotlin, the support for tuples has been deprecated in favor of using the data class to return multiple values.

Kotlin also provides Pair and Triple data classes that you can use to return up to 3 values.

For example, here’s an equivalent to the myTuple variable in Kotlin. Notice how the values are accessed as first, second, and third properties from the object:

val myTuple = Triple("apple", 2, false)

println(myTuple.first) // apple
println(myTuple.second) // 2
println(myTuple.third) // false

Or if you only have two values, you can use the Pair data class:

val myTuple = Pair("apple", 2)

println(myTuple.first) // apple
println(myTuple.second) // 2

These built-in data classes can be used as the return type of a function as shown below:

fun twoPairs(): Pair<Int, Int> {
    return Pair(2, 10)
}

val myPairs = twoPairs()
println(myPairs.first) // 2
println(myPairs.second) // 10

The returned data class instance from your function can also be destructured as follows:

fun twoPairs(): Pair<Int, Int> {
    return Pair(2, 10)
}

val (one, two) = twoPairs()

println(one) // 2
println(two) // 10

Finally, when you need to return more than 3 values from a function, you need to define your own data class and use it as the return type of the function.

The example below shows how the User data class is created to return 4 values from a function:

data class User(
    val name: String, 
    val age: Int,
    val gender: String,
    val nationality: String,
)

fun fetchUser(): User {
    return User(
        "John",
        25,
        "Male",
        "United States"
    )
}

val (name, age, gender, nation) = fetchUser()

And that’s how you can use Kotlin data class to return multiple values from a function.

Kotlin used to support tuples during its early development days, but after much progress, the support for tuples has been deprecated in favor of the data class feature.

The data class has two benefits over using tuples:

  • data class can be reused in many places in your source code
  • data class can use the destructuring declaration syntax to extract its values

The Kotlin destructuring declaration syntax allows you to declare variable(s) and assign the data class instance values as their values.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.