How to convert a class that implements java.lang.Comparable to implement Scala. Blocked?

Renames extends Comparable[A] to extends Ordered[A] and renames def compareTo to def compare enough or is there anything I need to take care of?

+4
source share
1 answer

You are right that all you have to do. Other methods in Ordered will use their default implementations, which look like this:

 def < (that: A): Boolean = (this compare that) < 0 def > (that: A): Boolean = (this compare that) > 0 def <= (that: A): Boolean = (this compare that) <= 0 def >= (that: A): Boolean = (this compare that) >= 0 def compareTo(that: A): Int = compare(that) 

The only thing that does not have a default implementation in Ordered is a comparison, which you will define using the old compareTo method. Should work if the above is what you want for other comparisons.

+7
source

Source: https://habr.com/ru/post/1333200/


All Articles