Reputation: 3497
Kotlin reference document said this example is valid.
https://kotlinlang.org/docs/reference/generics.html#upper-bounds
fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<T>
where T : Comparable<T>,
T : Cloneable {
return list.filter { it > threshold }.map { it.clone() }
}
But in Android studio 3.0, it shows thin red line under it.clone()
. And error message is:
Type inference failed. Expected type mismatch.
Required:List<T>
Found:List<Any>
Why this example cannot be compiled?
Upvotes: 0
Views: 699
Reputation: 82047
The problem is the use of clone()
, which is protected
as the compiler complains. The problem's already been discussed here: https://discuss.kotlinlang.org/t/is-the-documentation-correct/2925
Upvotes: 1