Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Kotlin: The Tuple Type

DZone's Guide to

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.

· Java Zone ·
Free Resource

Learn how to build stream processing applications in Java-includes reference application. Brought to you in partnership with Hazelcast.

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)


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.

Learn how to build distributed stream processing applications in Java that elastically scale to meet demand- includes reference application.  Brought to you in partnership with Hazelcast.

Topics:
java ,kotlin ,tuples

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}