DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Tuples and Records (Part 5): Performance Challenges
  • Tuples and Records (Part 4): Optimize React and Prevent Re-Renders

Trending

  • How AI Is Transforming Software Engineering and How Developers Can Take Advantage
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  1. DZone
  2. Coding
  3. Languages
  4. Kotlin: The Tuple Type

Kotlin: The Tuple Type

Get your feet wet with Tuples in Kotlin and how to easily define elements, deconstruct constituent types, and a few other tips to help you out.

By 
Biju Kunjummen user avatar
Biju Kunjummen
·
Jan. 05, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
28.7K Views

Join the DZone community and get the full member experience.

Join For Free

It is very easy to write a Tuple type with the expressiveness of Kotlin. My objective, expressed in tests, is the following:

  1. Be able to define a Tuple of up to 5 elements and be able to retrieve the elements using an index like a placeholder in a test expressed with 2 elements, like this:
  2. val tup = Tuple("elem1", "elem2")
    assertThat(tup._1).isEqualTo("elem1")
    assertThat(tup._2).isEqualTo("elem2")


  3. Be able to deconstruct the constituent types along the following lines:

  4. val tup = Tuple("elem1", "elem2")
    val (e1, e2) = tup
    
    assertThat(e1).isEqualTo("elem1")
    assertThat(e2).isEqualTo("elem2")


Implementation

The implementation for a Tuple of 2 elements is the following in its entirety:

data class Tuple2<out A, out B>(val _1: A, val _2: B)


A Kotlin data class provides all the underlying support of being able to retrieve the individual fields and the ability to destructure using an expression like this:

val (e1, e2) = Tuple2("elem1", "elem2")


All I need to do at this point is provide a helper that creates a Tuple of appropriate size based on the number of arguments provided, which I have defined as follows:

object Tuple {
    operator fun <A> invoke(_1: A): Tuple1<A> = Tuple1(_1)
    operator fun <A, B> invoke(_1: A, _2: B): Tuple2<A, B> = Tuple2(_1, _2)
    operator fun <A, B, C> invoke(_1: A, _2: B, _3: C): Tuple3<A, B, C> = Tuple3(_1, _2, _3)
    operator fun <A, B, C, D> invoke(_1: A, _2: B, _3: C, _4: D): Tuple4<A, B, C, D> = Tuple4(_1, _2, _3, _4)
    operator fun <A, B, C, D, E> invoke(_1: A, _2: B, _3: C, _4: D, _5: E): Tuple5<A, B, C, D, E> = Tuple5(_1, _2, _3, _4, _5)
}


Which allows me to define Tuples of different sizes using a construct that looks like this:

val tup2 = Tuple("elem1", "elem2")
val tup3 = Tuple("elem1", "elem2", "elem3")
val tup4 = Tuple("elem1", "elem2", "elem3", "elem4")


For a little more of a twist, typically a Pair type is an alias for Tuple with 2 elements, and Triple is an alias for a Tuple of 3 elements. This can be trivially defined in Kotlin the following way:

typealias Pair<A, B> = Tuple2<A, B>
typealias Triple<A, B, C> = Tuple3<A, B, C>


Simple indeed! A more filled-in sample is available in my GitHub repo here.

Tuple Kotlin (programming language)

Published at DZone with permission of Biju Kunjummen. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Tuples and Records (Part 5): Performance Challenges
  • Tuples and Records (Part 4): Optimize React and Prevent Re-Renders

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook