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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.