Triple by infix fun "to" (closed)

inline infix fun <T1, T2, T3> Pair<T1, T2>.to(third: T3) = Triple(first, second, third)
A to B to C

What if you wanted to create a Pair<Pair<A, B>, C>?

Currently you would use A to B to C but with your suggestion it would not be possible.

Triple creation is more demanded than nested pair.
Explicit constructor Pair(A to B, C) is clearer.

Triple(A, B, C) is shorter.

Bytecode is shorter. 8-(
Text (A to B to C) is shorter.

It doesn’t matter. This would be a backwards incompatible change since it would change the result of a to b to c. And I don’t think there are enough good arguments to warrant a breaking change.

1 Like

I can create an alias t for Triple and it will be shorter. My point is that your proposal does not bring anything useful. In fact Triple usage is rather rare case. If you are using them a lot, probably you are doing something wrong. Try data classes instead.


Triple is the data class. :sunglasses:

That’s convincing.

I have seen tons of terrible code that overuses Pairs and Triples. They do not mean anything by themselves, and they should be only used in relation to maps and some algorithms.

Like: val data = result.first()

1 Like

True, anytime when you have to use first, second or third in your code, it’s confusing, like unnamed variables.

Pair is useful in some (library) functions such as mapOf to pass parameter pairs using to helper. But I rarely use it in own code. And the Triple almost never.

If you need class with 3 fields, create your own with properly named fields.