Why does Pair not implement Iterable?


I was just wondering if there is a specific reason why Pair doesn't implement Iterable. The case I encountered was to flatten a list of pairs (List<Pair<A,A>> to List<A>). I've written the following

listOfPairs flatMap { listOf(it.first, it.second) }

If Pair implemented Iterable it would have been

listOfPairs flatMap { it } // or if there was an identity function listOfPairs flatMap id

But I'm sure there would be other use cases for Pair implementing Iterable, too.

Btw, if there is a better way to flatten a list of pairs, please let me know.



Only Pair<A, A> could possibly implement Iterable<A>, otherwise there's no sensible type argument for that supertype. Since pairs are designed as heterogenous (elements can and usually do have different types), they can not implement Iterable.

The code you worte seems good enough to me, and is unlikely to be repeated often. If it does repeat often, you can extract it as a function: Pair<A, A>.toList()


That makes sense. Thanks for the answer, Andrey!