Why does Scala's RichInt extends Comparable[Int] instead of Comparable[RichInt]?
I'm new to Scala. When browsing Scala library code, I noticed that class RichInt extends ScalaNumberProxy[Int],
ScalaNumberProxy extends OrderedProxy, OrderedProxy extends Ordered, and Ordered extends java.lang.Comparable.
So my understanding is that RichInt (indirectly) extends java.lang.Comparable[Int].
My question is why doesn't it extends java.lang.Comparable[RichInt]?
The reason that I raise this question is in Java, I would write code like this:
class A implements Comparable<A> {
@Override
public int compareTo(A other) {
//implement the method.
}
....
}
Firstly we need to know RichInt is used for supplying helper functions for Int type by implicit conversions defined in the Predef:
@inline implicit def intWrapper(x: Int) = new runtime.RichInt(x)
So you can use it like:
2.doubleValue()
2.shortValue()
...
And for your question: why doesn't it extends java.lang.Comparable[RichInt]
Because we actually is operating on the Int type, not the RichInt type. like:
2.compare(3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With