diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index 63c9893a82e..26f9ce09cd9 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -7,6 +7,356 @@ package kotlin import java.util.* +/** + * Returns 1st *element* from the collection. + */ +public inline fun Array.component1(): T { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun BooleanArray.component1(): Boolean { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun ByteArray.component1(): Byte { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun CharArray.component1(): Char { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun DoubleArray.component1(): Double { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun FloatArray.component1(): Float { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun IntArray.component1(): Int { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun LongArray.component1(): Long { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun ShortArray.component1(): Short { + return get(0) +} + +/** + * Returns 1st *element* from the collection. + */ +public inline fun List.component1(): T { + return get(0) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun Array.component2(): T { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun BooleanArray.component2(): Boolean { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun ByteArray.component2(): Byte { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun CharArray.component2(): Char { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun DoubleArray.component2(): Double { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun FloatArray.component2(): Float { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun IntArray.component2(): Int { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun LongArray.component2(): Long { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun ShortArray.component2(): Short { + return get(1) +} + +/** + * Returns 2nd *element* from the collection. + */ +public inline fun List.component2(): T { + return get(1) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun Array.component3(): T { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun BooleanArray.component3(): Boolean { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun ByteArray.component3(): Byte { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun CharArray.component3(): Char { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun DoubleArray.component3(): Double { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun FloatArray.component3(): Float { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun IntArray.component3(): Int { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun LongArray.component3(): Long { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun ShortArray.component3(): Short { + return get(2) +} + +/** + * Returns 3rd *element* from the collection. + */ +public inline fun List.component3(): T { + return get(2) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun Array.component4(): T { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun BooleanArray.component4(): Boolean { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun ByteArray.component4(): Byte { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun CharArray.component4(): Char { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun DoubleArray.component4(): Double { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun FloatArray.component4(): Float { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun IntArray.component4(): Int { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun LongArray.component4(): Long { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun ShortArray.component4(): Short { + return get(3) +} + +/** + * Returns 4th *element* from the collection. + */ +public inline fun List.component4(): T { + return get(3) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun Array.component5(): T { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun BooleanArray.component5(): Boolean { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun ByteArray.component5(): Byte { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun CharArray.component5(): Char { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun DoubleArray.component5(): Double { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun FloatArray.component5(): Float { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun IntArray.component5(): Int { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun LongArray.component5(): Long { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun ShortArray.component5(): Short { + return get(4) +} + +/** + * Returns 5th *element* from the collection. + */ +public inline fun List.component5(): T { + return get(4) +} + /** * Returns true if *element* is found in the collection */ diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index d3db7f372b5..197f8cceac2 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -471,4 +471,42 @@ class CollectionTest { expect(arrayListOf(1, 2, 3)) { list.sort() } expect(arrayListOf(2, 3, 1)) { list } } + + test fun decomposeFirst() { + val (first) = listOf(1, 2) + assertEquals(first, 1) + } + + test fun decomposeSplit() { + val (key, value) = "key = value".split('=').map { it.trim() } + assertEquals(key, "key") + assertEquals(value, "value") + } + + test fun decomposeList() { + val (a, b, c, d, e) = listOf(1, 2, 3, 4, 5) + assertEquals(a, 1) + assertEquals(b, 2) + assertEquals(c, 3) + assertEquals(d, 4) + assertEquals(e, 5) + } + + test fun decomposeArray() { + val (a, b, c, d, e) = array(1, 2, 3, 4, 5) + assertEquals(a, 1) + assertEquals(b, 2) + assertEquals(c, 3) + assertEquals(d, 4) + assertEquals(e, 5) + } + + test fun decomposeIntArray() { + val (a, b, c, d, e) = intArray(1, 2, 3, 4, 5) + assertEquals(a, 1) + assertEquals(b, 2) + assertEquals(c, 3) + assertEquals(d, 4) + assertEquals(e, 5) + } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 007c92b6b27..0451084a52b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -434,5 +434,41 @@ fun elements(): List { } } + templates add f("component1()") { + inline(true) + doc { "Returns 1st *element* from the collection." } + returns("T") + body { "return get(0)" } + only(Lists, ArraysOfObjects, ArraysOfPrimitives) + } + templates add f("component2()") { + inline(true) + doc { "Returns 2nd *element* from the collection." } + returns("T") + body { "return get(1)" } + only(Lists, ArraysOfObjects, ArraysOfPrimitives) + } + templates add f("component3()") { + inline(true) + doc { "Returns 3rd *element* from the collection." } + returns("T") + body { "return get(2)" } + only(Lists, ArraysOfObjects, ArraysOfPrimitives) + } + templates add f("component4()") { + inline(true) + doc { "Returns 4th *element* from the collection." } + returns("T") + body { "return get(3)" } + only(Lists, ArraysOfObjects, ArraysOfPrimitives) + } + templates add f("component5()") { + inline(true) + doc { "Returns 5th *element* from the collection." } + returns("T") + body { "return get(4)" } + only(Lists, ArraysOfObjects, ArraysOfPrimitives) + } + return templates } \ No newline at end of file