# Better Tuples

**URL:** <https://discuss.kotlinlang.org/t/better-tuples/17800>\
**Category:** Language Design\
**Created:** [May 22, 2020, 8:03pm UTC](https://discuss.kotlinlang.org/t/better-tuples/17800 "2020-05-22T20:03:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterhenryd](https://avatars.discourse-cdn.com/v4/letter/p/90db22/32.png) [@peterhenryd](https://discuss.kotlinlang.org/u/peterhenryd)\
**Post date:** [May 22, 2020, 8:03pm UTC](https://discuss.kotlinlang.org/t/better-tuples/17800/1 "2020-05-22T20:03:01Z")

</div>

In Kotlin, you can already do stuff like `"hello" to "hi" to "greetings"`, which will result in a `Pair<String, Pair<String, String>>`, now you can do this continually to get a tuple of _x_. This means there is no need for the Triple tuple.

To use this easily, add some utility functions. Like Pair#forEach, which would like a bit like this:

```auto
fun <A, B> Pair<A, B>.forEach(context: (Any?) -> Unit) {
    for (any in arrayOf(first, second)) {
        if (any is Pair<*, *>) {
            any.forEach(context)
        } else {
            any.apply(context)
        }
    }
}

```

Tuple creation from a List could look something like this.

```auto
fun tupleOf(list: List<Any?>): Pair<Any?, Any?> {
    return if (list.size >= 2) 
        Pair(list[0], tupleOf(list.drop(1))) 
    else
        Pair(list.getOrNull(0), list.getOrNull(1))
}

```

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [May 22, 2020, 10:16pm UTC](https://discuss.kotlinlang.org/t/better-tuples/17800/2 "2020-05-22T22:16:40Z")

</div>

Why not just use `listOf(...)` instead? Why do you need a tuple object here?

---

<div class="post-metadata">

**Author:** ![solomonu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/solomonu/32/6542_2.png) [@solomonu](https://discuss.kotlinlang.org/u/solomonu)\
**Post date:** [May 30, 2020, 2:43pm UTC](https://discuss.kotlinlang.org/t/better-tuples/17800/3 "2020-05-30T14:43:40Z")

</div>

This sounds like [`cons` cell](https://en.wikipedia.org/wiki/Cons)–based linked lists.

---

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [May 30, 2020, 6:07pm UTC](https://discuss.kotlinlang.org/t/better-tuples/17800/4 "2020-05-30T18:07:47Z")

</div>

If not for the automatic bounds checking, it would be great if tuples were subclasses of `Array<Any?>`
