Is the Kotlin documentation correct?

Viewed 215

Is the code (show below) correct? It was take from page 63 of the Kotlin-docs.pdf, which is also the last code snippet of https://kotlinlang.org/docs/reference/generics.html

 fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<T> 
 where T : Comparable, T : Cloneable {
 return list.filter { it > threshold }.map { it.clone() }
 }

Taken by as is, the compiler fails with: 1. One type argument expected for interface Comparable defined in kotlin 2. Type inference failed. Expected type mismatch: inferred type is List but List was expected 3. Cannot access 'clone': it is protected in 'Cloneable'

The first two errors are easily resolved by changing the code to the following:

 fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<Any>
        where T : Comparable<in T>, T : Cloneable {
    return list.filter { it > threshold }.map { it.clone() }
}

I still get the following error: Cannot access 'clone': it is protected in 'Cloneable'

I'm using Kotlin 1.1.2-release-IJ2017.1-1

Am I missing something? or is there an error in the documentation?

Thanks.

1 Answers
Related