Created
August 23, 2019 00:33
-
-
Save ItsDoot/989a0d850a3a8f0d222336a0279789e0 to your computer and use it in GitHub Desktop.
recursively right-expanding kotlin tuple type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RTuple<out A, out B>(val head: A, val tail: B) { | |
| companion object { | |
| operator fun <A, B, C> invoke(a: A, b: B, c: C): RTuple<A, RTuple<B, C>> = | |
| RTuple(a, RTuple(b, c)) | |
| operator fun <A, B, C, D> invoke(a: A, b: B, c: C, d: D): RTuple<A, RTuple<B, RTuple<C, D>>> = | |
| RTuple(a, RTuple(b, RTuple(c, d))) | |
| operator fun <A, B, C, D, E> invoke(a: A, b: B, c: C, d: D, e: E): RTuple<A, RTuple<B, RTuple<C, RTuple<D, E>>>> = | |
| RTuple(a, RTuple(b, RTuple(c, RTuple(d, e)))) | |
| operator fun <A, B, C, D, E, F> invoke(a: A, b: B, c: C, d: D, e: E, f: F): RTuple<A, RTuple<B, RTuple<C, RTuple<D, RTuple<E, F>>>>> = | |
| RTuple(a, RTuple(b, RTuple(c, RTuple(d, RTuple(e, f))))) | |
| operator fun <A, B, C, D, E, F, G> invoke(a: A, b: B, c: C, d: D, e: E, f: F, g: G): RTuple<A, RTuple<B, RTuple<C, RTuple<D, RTuple<E, RTuple<F, G>>>>>> = | |
| RTuple(a, RTuple(b, RTuple(c, RTuple(d, RTuple(e, RTuple(f, g)))))) | |
| operator fun <A, B, C, D, E, F, G, H> invoke(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): RTuple<A, RTuple<B, RTuple<C, RTuple<D, RTuple<E, RTuple<F, RTuple<G, H>>>>>>> = | |
| RTuple(a, RTuple(b, RTuple(c, RTuple(d, RTuple(e, RTuple(f, RTuple(g, h))))))) | |
| } | |
| override fun equals(other: Any?): Boolean = when { | |
| this === other -> true | |
| other is RTuple<*, *> -> this.head == other.head && this.tail == other.tail | |
| else -> false | |
| } | |
| override fun hashCode(): Int = | |
| 31 * (head?.hashCode() ?: 0) + (tail?.hashCode() ?: 0) | |
| override fun toString(): String { | |
| val builder = StringBuilder() | |
| .append('(').append(head).append(", ") | |
| var curTail: Any? = tail | |
| while (curTail is RTuple<*, *>) { | |
| builder.append(curTail.head).append(", ") | |
| curTail = curTail.tail | |
| } | |
| builder.append(curTail).append(')') | |
| return builder.toString() | |
| } | |
| } | |
| @JvmName("_1") | |
| inline operator fun <reified A> RTuple<A, *>.component1(): A = | |
| head | |
| @JvmName("_2a") | |
| inline operator fun <reified B> RTuple<*, B>.component2(): B = | |
| tail | |
| @JvmName("_2b") | |
| inline operator fun <reified B> RTuple<*, RTuple<B, *>>.component2(): B = | |
| tail.head | |
| @JvmName("_3a") | |
| inline operator fun <reified C> RTuple<*, RTuple<*, C>>.component3(): C = | |
| tail.tail | |
| @JvmName("_3b") | |
| inline operator fun <reified C> RTuple<*, RTuple<*, RTuple<C, *>>>.component3(): C = | |
| tail.tail.head | |
| @JvmName("_4a") | |
| inline operator fun <reified D> RTuple<*, RTuple<*, RTuple<*, D>>>.component4(): D = | |
| tail.tail.tail | |
| @JvmName("_4b") | |
| inline operator fun <reified D> RTuple<*, RTuple<*, RTuple<*, RTuple<D, *>>>>.component4(): D = | |
| tail.tail.tail.head | |
| @JvmName("_5a") | |
| inline operator fun <reified E> RTuple<*, RTuple<*, RTuple<*, RTuple<*, E>>>>.component5(): E = | |
| tail.tail.tail.tail | |
| @JvmName("_5b") | |
| inline operator fun <reified E> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<E, *>>>>>.component5(): E = | |
| tail.tail.tail.tail.head | |
| @JvmName("_6a") | |
| inline operator fun <reified F> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, F>>>>>.component6(): F = | |
| tail.tail.tail.tail.tail | |
| @JvmName("_6b") | |
| inline operator fun <reified F> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<F, *>>>>>>.component6(): F = | |
| tail.tail.tail.tail.tail.head | |
| @JvmName("_7a") | |
| inline operator fun <reified G> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, G>>>>>>.component7(): G = | |
| tail.tail.tail.tail.tail.tail | |
| @JvmName("_7b") | |
| inline operator fun <reified G> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<G, *>>>>>>>.component7(): G = | |
| tail.tail.tail.tail.tail.tail.head | |
| @JvmName("_8a") | |
| inline operator fun <reified H> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, H>>>>>>>.component8(): H = | |
| tail.tail.tail.tail.tail.tail.tail | |
| @JvmName("_8b") | |
| inline operator fun <reified H> RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<*, RTuple<H, *>>>>>>>>.component8(): H = | |
| tail.tail.tail.tail.tail.tail.tail.head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment