I was implementing custom list class MyList<T>
in kotlin. In that, I wanted to add insertSorted
function, which inserts a new element into the list in sorted order. For that, T
must implement comparator. So the prototype of that function will be fun <C> insertSorted(ele: C) where C:T, C:Comparable<T>
But this is giving me Type parameter cannot have any other bounds if it's bounded by another type parameter error. I am not understanding what this error is. Also, this question did not help me much.
PS: The type I am passing to that function is declared as class MyClass : Comparator<MyClass>
. So the bound where C:T, C:Comparator<T>
is valid I guess.
For the meaning of the error, see this question:
Why can't type parameter in Kotlin have any other bounds if it's bounded by another type parameter?
But if your custom list contains elements of type T
and you want to compare them, then T
should implement Comparable<T>
.
So this should be all you need:
class MyList<T: Comparable<T>> {
fun insertSorted(ele: T) {
}
}