I wanted to implement min()
/max()
aliases for the Kotlin's Comparable<T>coerceAtLeast()
/coerceAtMost()
, if nothing else for the exercise of extending an Interface (I've only extended classes so far).
I tried this:
fun <T>Comparable<T>.max(other:T) : T {
return this.coerceAtLeast(other)
}
But I get the following error:
Type inference failed: Cannot infer type parameter T in fun <T : Comparable<T#1 (type parameter of kotlin.ranges.coerceAtLeast)>> T#1.coerceAtLeast(minimumValue: T#1): T#1
None of the following substitutions
receiver: Any? arguments: (Any?)
receiver: Comparable<T#2 (type parameter of com.nelsonirrigation.twig.plans.extensions.max)> arguments: (Comparable<T#2>)
receiver: T#2 arguments: (T#2)
receiver: Comparable<Comparable<T#2>> arguments: (Comparable<Comparable<T#2>>)
can be applied to
receiver: Comparable<T#2> arguments: (T#2)
At which point my limited understanding of Kotlin generics basically overflowed. Is what I'm trying to do achievable? What is the piece of the puzzle I'm missing?