KT-20357 Add samples for elementAtOrNull
This commit is contained in:
committed by
Ilya Gorbunov
parent
ad9b700ec4
commit
7d5efe7f51
@@ -605,6 +605,8 @@ public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> C
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Array<out T>.elementAtOrNull(index: Int): T? {
|
||||
@@ -613,6 +615,8 @@ public inline fun <T> Array<out T>.elementAtOrNull(index: Int): T? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.elementAtOrNull(index: Int): Byte? {
|
||||
@@ -621,6 +625,8 @@ public inline fun ByteArray.elementAtOrNull(index: Int): Byte? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.elementAtOrNull(index: Int): Short? {
|
||||
@@ -629,6 +635,8 @@ public inline fun ShortArray.elementAtOrNull(index: Int): Short? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.elementAtOrNull(index: Int): Int? {
|
||||
@@ -637,6 +645,8 @@ public inline fun IntArray.elementAtOrNull(index: Int): Int? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.elementAtOrNull(index: Int): Long? {
|
||||
@@ -645,6 +655,8 @@ public inline fun LongArray.elementAtOrNull(index: Int): Long? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.elementAtOrNull(index: Int): Float? {
|
||||
@@ -653,6 +665,8 @@ public inline fun FloatArray.elementAtOrNull(index: Int): Float? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.elementAtOrNull(index: Int): Double? {
|
||||
@@ -661,6 +675,8 @@ public inline fun DoubleArray.elementAtOrNull(index: Int): Double? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? {
|
||||
@@ -669,6 +685,8 @@ public inline fun BooleanArray.elementAtOrNull(index: Int): Boolean? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.elementAtOrNull(index: Int): Char? {
|
||||
|
||||
@@ -116,6 +116,8 @@ public inline fun <T> List<T>.elementAtOrElse(index: Int, defaultValue: (Int) ->
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
public fun <T> Iterable<T>.elementAtOrNull(index: Int): T? {
|
||||
if (this is List)
|
||||
@@ -134,6 +136,8 @@ public fun <T> Iterable<T>.elementAtOrNull(index: Int): T? {
|
||||
|
||||
/**
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this list.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> List<T>.elementAtOrNull(index: Int): T? {
|
||||
|
||||
@@ -60,6 +60,8 @@ public fun <T> Sequence<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T)
|
||||
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
public fun <T> Sequence<T>.elementAtOrNull(index: Int): T? {
|
||||
if (index < 0)
|
||||
|
||||
@@ -38,6 +38,8 @@ public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -
|
||||
|
||||
/**
|
||||
* Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
|
||||
*
|
||||
* @sample samples.collections.Collections.Usage.elementAtOrNull
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence.elementAtOrNull(index: Int): Char? {
|
||||
|
||||
@@ -498,5 +498,16 @@ class Collections {
|
||||
val emptyList = emptyList<Int>()
|
||||
assertFails { emptyList.elementAt(0) }
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun elementAtOrNull() {
|
||||
val list = listOf(1, 2, 3)
|
||||
assertPrints(list.elementAtOrNull(0), "1")
|
||||
assertPrints(list.elementAtOrNull(2), "3")
|
||||
assertPrints(list.elementAtOrNull(3), "null")
|
||||
|
||||
val emptyList = emptyList<Int>()
|
||||
assertPrints(emptyList.elementAtOrNull(0), "null")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user