我想为 Kotlin 的/实现min()
/别名,如果没有其他扩展接口的练习(到目前为止我只扩展了类)。max()
Comparable<T>coerceAtLeast()
coerceAtMost()
我试过这个:
fun <T>Comparable<T>.max(other:T) : T {
return this.coerceAtLeast(other)
}
但我收到以下错误:
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)
至此,我对 Kotlin 泛型的有限理解基本溢出了。我正在努力做的事情可以实现吗?我缺少的拼图是什么?