I’m trying to define a trait that describes operators based on other operator. Something like this:
trait LessThanComparable[T] {
def < (that: T) : Boolean
def > (that: T) = that < this
}
Then I use it:
class Example(val x : Int) extends LessThanComparable[Example] {
def < (that: Example) = x < that.x
}
But I get this: value < is not a member of type parameter T
How can I say that that and this are of the same Type? Or am I trying something impossible?
I think this is what you want:
In order to be able to say
that < this, two things must hold.thatmust have a<method that accepts aT, or in other words,thatmust be aLessThanComparable[T]. We can ensure this by saying thatTmust be a subclass of LessThanComparable[T], orT <: LessThanComparable[T].thismust be aT. We can ensure this by using a self typethis: T =>.So then,