Generate indexOf and lastIndexOf extensions for List.
This commit is contained in:
@@ -271,6 +271,13 @@ public fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.indexOf(element: T):
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first index of [element], or -1 if the list does not contain element.
|
||||
*/
|
||||
public fun <@kotlin.internal.OnlyInputTypes T> List<T>.indexOf(element: T): Int {
|
||||
return indexOf(element)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first index of [element], or -1 if the collection does not contain element.
|
||||
*/
|
||||
@@ -282,6 +289,17 @@ public fun <T> Iterable<T>.indexOf(element: T): Int {
|
||||
return indexOf(element as Any?)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first index of [element], or -1 if the collection does not contain element.
|
||||
*/
|
||||
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)"))
|
||||
@kotlin.jvm.JvmName("indexOfAny")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
public fun <T> List<T>.indexOf(element: T): Int {
|
||||
return indexOf(element as Any?)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element.
|
||||
*/
|
||||
@@ -433,6 +451,13 @@ public fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.lastIndexOf(element:
|
||||
return lastIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of [element], or -1 if the list does not contain element.
|
||||
*/
|
||||
public fun <@kotlin.internal.OnlyInputTypes T> List<T>.lastIndexOf(element: T): Int {
|
||||
return lastIndexOf(element)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of [element], or -1 if the collection does not contain element.
|
||||
*/
|
||||
@@ -444,6 +469,17 @@ public fun <T> Iterable<T>.lastIndexOf(element: T): Int {
|
||||
return lastIndexOf(element as Any?)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of [element], or -1 if the collection does not contain element.
|
||||
*/
|
||||
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)"))
|
||||
@kotlin.jvm.JvmName("lastIndexOfAny")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
public fun <T> List<T>.lastIndexOf(element: T): Int {
|
||||
return lastIndexOf(element as Any?)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of [element], or -1 if the collection does not contain element.
|
||||
* Allows to overcome type-safety restriction of `lastIndexOf` that requires to pass an element of type `T`.
|
||||
|
||||
@@ -77,31 +77,6 @@ public inline fun <E> MutableCollection<E>.retainAllRaw(elements: Collection<Any
|
||||
public fun <E> MutableCollection<E>.retainAll(elements: Collection<Any?>): Boolean = retainAll(elements as Collection<Any?>)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
|
||||
* element is not contained in the list.
|
||||
* Allows to overcome type-safety restriction of `indexOf` that requires to pass an element of type `T`.
|
||||
*/
|
||||
public fun <@kotlin.internal.OnlyInputTypes T> List<T>.indexOf(element: T): Int = indexOf(element)
|
||||
|
||||
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)"))
|
||||
@kotlin.jvm.JvmName("indexOfAny")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public fun <E> List<E>.indexOf(element: Any?): Int = indexOf(element as Any?)
|
||||
|
||||
/**
|
||||
* Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
|
||||
* element is not contained in the list.
|
||||
* Allows to overcome type-safety restriction of `lastIndexOf` that requires to pass an element of type `T`.
|
||||
*/
|
||||
public fun <@kotlin.internal.OnlyInputTypes T> List<T>.lastIndexOf(element: T): Int = lastIndexOf(element)
|
||||
|
||||
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)"))
|
||||
@kotlin.jvm.JvmName("lastIndexOfAny")
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public fun <E> List<E>.lastIndexOf(element: Any?): Int = lastIndexOf(element as Any?)
|
||||
|
||||
|
||||
@Deprecated("Use operator 'get' instead", ReplaceWith("this[index]"))
|
||||
public fun CharSequence.charAt(index: Int): Char = this[index]
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
templates add f("indexOf(element: T)") {
|
||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
||||
doc { f -> "Returns first index of [element], or -1 if the ${f.collection} does not contain element." }
|
||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||
returns("Int")
|
||||
@@ -102,10 +102,11 @@ fun elements(): List<GenericFunction> {
|
||||
return -1
|
||||
"""
|
||||
}
|
||||
body(Lists) { "return indexOf(element)" }
|
||||
}
|
||||
|
||||
templates add f("indexOf(element: T)") {
|
||||
only(Iterables, Sequences, ArraysOfObjects)
|
||||
only(Iterables, Sequences, ArraysOfObjects, Lists)
|
||||
doc { "Returns first index of [element], or -1 if the collection does not contain element." }
|
||||
returns("Int")
|
||||
deprecate { f -> with(DocExtensions) { Deprecation("${f.collection.capitalize()} and element have incompatible types. Upcast element to Any? if you're sure.", "indexOf(element as Any?)") } }
|
||||
@@ -133,7 +134,7 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
templates add f("lastIndexOf(element: T)") {
|
||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
||||
doc { f -> "Returns last index of [element], or -1 if the ${f.collection} does not contain element." }
|
||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||
returns("Int")
|
||||
@@ -179,10 +180,11 @@ fun elements(): List<GenericFunction> {
|
||||
return -1
|
||||
"""
|
||||
}
|
||||
body(Lists) { "return lastIndexOf(element)" }
|
||||
}
|
||||
|
||||
templates add f("lastIndexOf(element: T)") {
|
||||
only(Iterables, Sequences, ArraysOfObjects)
|
||||
only(Iterables, Sequences, ArraysOfObjects, Lists)
|
||||
doc { "Returns last index of [element], or -1 if the collection does not contain element." }
|
||||
returns("Int")
|
||||
deprecate { f -> with(DocExtensions) { Deprecation("${f.collection.capitalize()} and element have incompatible types. Upcast element to Any? if you're sure.", "lastIndexOf(element as Any?)") } }
|
||||
|
||||
Reference in New Issue
Block a user