Apply @OnlyInputTypes on type parameter for contains, indexOf, lastIndexOf extensions for Iterables, Sequences and Arrays instead of @NoInfer on element parameter.

Provide covariant extensions annotated with @OnlyInputTypes:
 - Collection<T>: containsAll(Collection<T>),
 - MutableCollection<out T>: remove(T), removeAll, retainAll (Collection<T>),
 - List<T>: indexOf(T), lastIndexOf(T)
 - Map<out K, V>: get(K), containsKey(K), contains(K)
 - Map<K, V>: containsValue(V)
 - MutableMap<out K, V>: remove(K)
All *Raw extensions are deprecated.
This commit is contained in:
Ilya Gorbunov
2015-11-22 20:55:54 +03:00
parent f6616e6e42
commit a25f23a286
7 changed files with 161 additions and 80 deletions
+17 -12
View File
@@ -55,7 +55,7 @@ public inline operator fun <T> List<T>.component5(): T {
/**
* Returns `true` if [element] is found in the collection.
*/
public operator fun <T> Iterable<T>.contains(element: @kotlin.internal.NoInfer T): Boolean {
public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.contains(element: T): Boolean {
if (this is Collection)
return contains(element)
return indexOf(element) >= 0
@@ -64,20 +64,21 @@ public operator fun <T> Iterable<T>.contains(element: @kotlin.internal.NoInfer T
/**
* Returns `true` if [element] is found in the collection.
*/
@Deprecated("Use 'containsRaw' instead.", ReplaceWith("containsRaw(element)"))
@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("contains(element as Any?)"))
@kotlin.jvm.JvmName("containsAny")
@kotlin.internal.LowPriorityInOverloadResolution
public operator fun <T> Iterable<T>.contains(element: T): Boolean {
return containsRaw(element)
return contains(element as Any?)
}
/**
* Returns `true` if [element] is found in the collection.
* Allows to overcome type-safety restriction of `contains` that requires to pass an element of type `T`.
*/
@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("contains(element as Any?)"))
@Suppress("NOTHING_TO_INLINE")
public inline fun Iterable<*>.containsRaw(element: Any?): Boolean {
return contains<Any?>(element)
return contains(element as Any?)
}
/**
@@ -259,7 +260,7 @@ public fun <T> List<T>.getOrNull(index: Int): T? {
/**
* Returns first index of [element], or -1 if the collection does not contain element.
*/
public fun <T> Iterable<T>.indexOf(element: @kotlin.internal.NoInfer T): Int {
public fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.indexOf(element: T): Int {
if (this is List) return this.indexOf(element)
var index = 0
for (item in this) {
@@ -273,12 +274,12 @@ public fun <T> Iterable<T>.indexOf(element: @kotlin.internal.NoInfer T): Int {
/**
* Returns first index of [element], or -1 if the collection does not contain element.
*/
@Deprecated("Use 'indexOfRaw' instead.", ReplaceWith("indexOfRaw(element)"))
@Deprecated("Collection 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> Iterable<T>.indexOf(element: T): Int {
return indexOfRaw(element)
return indexOf(element as Any?)
}
/**
@@ -336,15 +337,17 @@ public inline fun <T> List<T>.indexOfLast(predicate: (T) -> Boolean): Int {
* Returns first index of [element], or -1 if the collection does not contain element.
* Allows to overcome type-safety restriction of `indexOf` that requires to pass an element of type `T`.
*/
@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)"))
@Suppress("NOTHING_TO_INLINE")
public inline fun Iterable<*>.indexOfRaw(element: Any?): Int {
return indexOf<Any?>(element)
return indexOf(element as Any?)
}
/**
* Returns first index of [element], or -1 if the list does not contain element.
* Allows to overcome type-safety restriction of `indexOf` that requires to pass an element of type `T`.
*/
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("indexOf(element as Any?)"))
@Suppress("NOTHING_TO_INLINE")
public inline fun <T> List<T>.indexOfRaw(element: Any?): Int {
return (this as List<Any?>).indexOf(element)
@@ -418,7 +421,7 @@ public inline fun <T> List<T>.last(predicate: (T) -> Boolean): T {
/**
* Returns last index of [element], or -1 if the collection does not contain element.
*/
public fun <T> Iterable<T>.lastIndexOf(element: @kotlin.internal.NoInfer T): Int {
public fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.lastIndexOf(element: T): Int {
if (this is List) return this.lastIndexOf(element)
var lastIndex = -1
var index = 0
@@ -433,27 +436,29 @@ public fun <T> Iterable<T>.lastIndexOf(element: @kotlin.internal.NoInfer T): Int
/**
* Returns last index of [element], or -1 if the collection does not contain element.
*/
@Deprecated("Use 'indexOfRaw' instead.", ReplaceWith("indexOfRaw(element)"))
@Deprecated("Collection 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> Iterable<T>.lastIndexOf(element: T): Int {
return indexOfRaw(element)
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`.
*/
@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)"))
@Suppress("NOTHING_TO_INLINE")
public inline fun Iterable<*>.lastIndexOfRaw(element: Any?): Int {
return lastIndexOf<Any?>(element)
return lastIndexOf(element as Any?)
}
/**
* Returns last index of [element], or -1 if the list does not contain element.
* Allows to overcome type-safety restriction of `lastIndexOf` that requires to pass an element of type `T`.
*/
@Deprecated("List and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("lastIndexOf(element as Any?)"))
@Suppress("NOTHING_TO_INLINE")
public inline fun <T> List<T>.lastIndexOfRaw(element: Any?): Int {
return (this as List<Any?>).lastIndexOf(element)