Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin `Pair` nested destructuring
@file:Suppress("NonAsciiCharacters")
package me.eugeniomarletti.util.destructuring.pair.nested
object NoDestructuring
typealias PairA<A> = Pair<A, *>
typealias PairB<B> = Pair<*, B>
//region nesting level: 1
private typealias A<A> = PairA<A>
private typealias B<B> = PairB<B>
/** No need for [Pair.component1] & [Pair.component2] since [Pair] is a data class */
//endregion
//region nesting level: 2
private typealias ᴬ<ᴬ> = PairA<ᴬ>
private typealias ᴮ<ᴮ> = PairB<ᴮ>
operator fun <Aᴬ> A<ᴬ<Aᴬ>>.component3() = this
.let { (A, _) -> A }
.let { (ᴬ, _) -> ᴬ }
operator fun <Aᴮ> A<ᴮ<Aᴮ>>.component4() = this
.let { (A, _) -> A }
.let { (_, ᴮ) -> ᴮ }
operator fun <Bᴬ> B<ᴬ<Bᴬ>>.component5() = this
.let { (_, B) -> B }
.let { (ᴬ, _) -> ᴬ }
operator fun <Bᴮ> B<ᴮ<Bᴮ>>.component6() = this
.let { (_, B) -> B }
.let { (_, ᴮ) -> ᴮ }
/** Aᴬ **/ operator fun Pair<*, *>.component3() = NoDestructuring
/** Aᴮ **/ operator fun Pair<*, *>.component4() = NoDestructuring
/** Bᴬ **/ operator fun Pair<*, *>.component5() = NoDestructuring
/** Bᴮ **/ operator fun Pair<*, *>.component6() = NoDestructuring
//endregion
//region nesting level: 3
private typealias ᵅ<ᵅ> = PairA<ᵅ>
private typealias ᵇ<ᵇ> = PairB<ᵇ>
operator fun <Aᴬᵅ> A<ᴬ<ᵅ<Aᴬᵅ>>>.component7() = this
.let { (A, _) -> A }
.let { (ᴬ, _) -> ᴬ }
.let { (ᵅ, _) -> ᵅ }
operator fun <Aᴬᵇ> A<ᴬ<ᵇ<Aᴬᵇ>>>.component8() = this
.let { (A, _) -> A }
.let { (ᴬ, _) -> ᴬ }
.let { (_, ᵇ) -> ᵇ }
operator fun <Aᴮᵅ> A<ᴮ<ᵅ<Aᴮᵅ>>>.component9() = this
.let { (A, _) -> A }
.let { (_, ᴮ) -> ᴮ }
.let { (ᵅ, _) -> ᵅ }
operator fun <Aᴮᵇ> A<ᴮ<ᵇ<Aᴮᵇ>>>.component10() = this
.let { (A, _) -> A }
.let { (_, ᴮ) -> ᴮ }
.let { (_, ᵇ) -> ᵇ }
operator fun <Bᴬᵅ> B<ᴬ<ᵅ<Bᴬᵅ>>>.component11() = this
.let { (_, B) -> B }
.let { (ᴬ, _) -> ᴬ }
.let { (ᵅ, _) -> ᵅ }
operator fun <Bᴬᵇ> B<ᴬ<ᵇ<Bᴬᵇ>>>.component12() = this
.let { (_, B) -> B }
.let { (ᴬ, _) -> ᴬ }
.let { (_, ᵇ) -> ᵇ }
operator fun <Bᴮᵅ> B<ᴮ<ᵅ<Bᴮᵅ>>>.component13() = this
.let { (_, B) -> B }
.let { (_, ᴮ) -> ᴮ }
.let { (ᵅ, _) -> ᵅ }
operator fun <Bᴮᵇ> B<ᴮ<ᵇ<Bᴮᵇ>>>.component14() = this
.let { (_, B) -> B }
.let { (_, ᴮ) -> ᴮ }
.let { (_, ᵇ) -> ᵇ }
/** Aᴬᵅ **/ operator fun Pair<*, *>.component7() = NoDestructuring
/** Aᴬᵇ **/ operator fun Pair<*, *>.component8() = NoDestructuring
/** Aᴮᵅ **/ operator fun Pair<*, *>.component9() = NoDestructuring
/** Aᴮᵇ **/ operator fun Pair<*, *>.component10() = NoDestructuring
/** Bᴬᵅ **/ operator fun Pair<*, *>.component11() = NoDestructuring
/** Bᴬᵇ **/ operator fun Pair<*, *>.component12() = NoDestructuring
/** Bᴮᵅ **/ operator fun Pair<*, *>.component13() = NoDestructuring
/** Bᴮᵇ **/ operator fun Pair<*, *>.component14() = NoDestructuring
//endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.