Generate indexOf and lastIndexOf extensions for List.

This commit is contained in:
Ilya Gorbunov
2015-11-26 06:05:52 +03:00
parent c7d08ea599
commit de86e90103
3 changed files with 42 additions and 29 deletions
@@ -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]