https://kotlinlang.org logo
Title
r

Ruckus

06/06/2018, 9:31 PM
For some reason, I can't figure out what is going on here:
class Ref<K: Comparable<K>, T>(val key: K, val value: T) : Comparable<Ref<K, T>> {
    override fun compareTo(other: Ref<K, T>) = key.compareTo(other.key)
}

fun main(args: Array<String>) {
    val list = mutableListOf<Ref<Int, *>>()
    list.sorted()  // ERROR: Ref<Int, *> is not a subtype of Comparable<Ref<Int, *>>
}
Any ideas?
i

ilya.gorbunov

06/06/2018, 10:11 PM
Try declaring
Ref
as
class Ref<K: Comparable<K>, T>(val key: K, val value: T) : Comparable<Ref<K, *>> {
    override fun compareTo(other: Ref<K, *>) = key.compareTo(other.key)
}
r

Ruckus

06/06/2018, 10:22 PM
Ah, that makes sense.
Yep, it works. Thanks @ilya.gorbunov!