KT-20357 Add samples for elementAt
This commit is contained in:
committed by
Ilya Gorbunov
parent
40b3514a47
commit
ad9b700ec4
@@ -443,6 +443,8 @@ public operator fun CharArray.contains(element: Char): Boolean {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun <T> Array<out T>.elementAt(index: Int): T {
|
public inline fun <T> Array<out T>.elementAt(index: Int): T {
|
||||||
@@ -451,6 +453,8 @@ public inline fun <T> Array<out T>.elementAt(index: Int): T {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun ByteArray.elementAt(index: Int): Byte {
|
public inline fun ByteArray.elementAt(index: Int): Byte {
|
||||||
@@ -459,6 +463,8 @@ public inline fun ByteArray.elementAt(index: Int): Byte {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun ShortArray.elementAt(index: Int): Short {
|
public inline fun ShortArray.elementAt(index: Int): Short {
|
||||||
@@ -467,6 +473,8 @@ public inline fun ShortArray.elementAt(index: Int): Short {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun IntArray.elementAt(index: Int): Int {
|
public inline fun IntArray.elementAt(index: Int): Int {
|
||||||
@@ -475,6 +483,8 @@ public inline fun IntArray.elementAt(index: Int): Int {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun LongArray.elementAt(index: Int): Long {
|
public inline fun LongArray.elementAt(index: Int): Long {
|
||||||
@@ -483,6 +493,8 @@ public inline fun LongArray.elementAt(index: Int): Long {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun FloatArray.elementAt(index: Int): Float {
|
public inline fun FloatArray.elementAt(index: Int): Float {
|
||||||
@@ -491,6 +503,8 @@ public inline fun FloatArray.elementAt(index: Int): Float {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun DoubleArray.elementAt(index: Int): Double {
|
public inline fun DoubleArray.elementAt(index: Int): Double {
|
||||||
@@ -499,6 +513,8 @@ public inline fun DoubleArray.elementAt(index: Int): Double {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun BooleanArray.elementAt(index: Int): Boolean {
|
public inline fun BooleanArray.elementAt(index: Int): Boolean {
|
||||||
@@ -507,6 +523,8 @@ public inline fun BooleanArray.elementAt(index: Int): Boolean {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun CharArray.elementAt(index: Int): Char {
|
public inline fun CharArray.elementAt(index: Int): Char {
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.contains(ele
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
public fun <T> Iterable<T>.elementAt(index: Int): T {
|
public fun <T> Iterable<T>.elementAt(index: Int): T {
|
||||||
if (this is List)
|
if (this is List)
|
||||||
@@ -78,6 +80,8 @@ public fun <T> Iterable<T>.elementAt(index: Int): T {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this list.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this list.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun <T> List<T>.elementAt(index: Int): T {
|
public inline fun <T> List<T>.elementAt(index: Int): T {
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ public operator fun <@kotlin.internal.OnlyInputTypes T> Sequence<T>.contains(ele
|
|||||||
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this sequence.
|
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this sequence.
|
||||||
*
|
*
|
||||||
* The operation is _terminal_.
|
* The operation is _terminal_.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
public fun <T> Sequence<T>.elementAt(index: Int): T {
|
public fun <T> Sequence<T>.elementAt(index: Int): T {
|
||||||
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
|
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import kotlin.random.*
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence.
|
* Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Usage.elementAt
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun CharSequence.elementAt(index: Int): Char {
|
public inline fun CharSequence.elementAt(index: Int): Char {
|
||||||
|
|||||||
@@ -487,4 +487,16 @@ class Collections {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Usage {
|
||||||
|
@Sample
|
||||||
|
fun elementAt() {
|
||||||
|
val list = listOf(1, 2, 3)
|
||||||
|
assertPrints(list.elementAt(0), "1")
|
||||||
|
assertPrints(list.elementAt(2), "3")
|
||||||
|
assertFails { list.elementAt(3) }
|
||||||
|
|
||||||
|
val emptyList = emptyList<Int>()
|
||||||
|
assertFails { emptyList.elementAt(0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -242,6 +242,7 @@ object Elements : TemplateGroupBase() {
|
|||||||
} builder {
|
} builder {
|
||||||
val index = '$' + "index"
|
val index = '$' + "index"
|
||||||
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this ${f.collection}." }
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this ${f.collection}." }
|
||||||
|
sample("samples.collections.Collections.Usage.elementAt")
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user