I have this bit of code, which works:
val directions = rs.map(_.direction) // Direction extends Enumeration
directions == directions.sorted.reverse
I’d like to instead do something like this:
ratings.map(_.direction).isInBackwardsOrder
class RichSeq[T](seq: Seq[T]) {
def isInBackwardsOrder = seq == seq.sorted.reverse
}
object RichSeq {
implicit def seq2richSeq[T](seq: Seq[T]) = new RichSeq[T](seq)
}
I keep getting the following compilation error:
could not find implicit value for parameter ord: Ordering[T]
def isInBackwardsOrder = seq == seq.sorted.reverse
What I don’t understand is why it could find the implicit value for parameter ord, when it was in the original form, but cannot find it once I pull it into a utility class.
Thanks for the help,
Alex
In the original form, you had no generics.
directionsis aSeq[SomeWellKnownType], and at compile time, the compiler looks for anOrdering[SomeWellKnownType]in implicit scope, and finds one.On the other hand, in
RichSeq[T], the compiler must find an implicitOrdering[T]whereTis a type parameter. No way to do that. You must ensure that theOrderingwill be available when you create theRichSeq:There is a shortcut for that, especially if you just need
evin implicit scope without refrencing it explicitly, as in here :Then you have the exact same problem in your implicit method, which is generic too, with the same solution :
Then it should work. The
seq2richSeqimplicit conversion will kick in when an Ordering is available for the type of the elements in the Seq.