0

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?

Stanley Ko
  • 3,383
  • 3
  • 34
  • 60

1 Answers1

1

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

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196