Let's say I have a generic class:
class OrderedArray<T>(
private val items: Array<T>,
private val comparator: Comparator<in T>
) {
constructor(items: Array<T>) : this(items, naturalOrder<T>())
}
Of course this code does not compile as T
is not necessarily comparable. Is there a language construct available which bounds the generic parameter of a type on a constructor? How could I allow to construct an instance of my class without explicitly passing the comparator when a natural ordering is available?