암시 적 순서가있는 스칼라 컬렉션 메서드로 enrich-my-library 패턴을 사용하려고합니다.암시 적 순서를 스칼라 컬렉션 개체에 적용하는 메서드를 추가하는 방법
@Test
def testOrdering {
import ImplicitTest._
val mx = List(1, 2, 7, 1, 4, 8, 2, 5, 47, 2, 7).myMinBy(_.toDouble)
// ...but this works:
// val mx = List(1, 2, 7, 1, 4, 8, 2, 5, 47, 2, 7).minBy(_.toDouble)
println(mx)
}
나에게이 컴파일 오류를 제공합니다 :
object ImplicitTest {
implicit def RichTraversableOnce[A](t: TraversableOnce[A]): RichTraversableOnce[A] =
new RichTraversableOnce[A](t)
class RichTraversableOnce[A](val t: TraversableOnce[A]) {
def myMinBy[B >: A](f: A => B)(implicit cmp: Ordering[B]): A = {
if (t.isEmpty)
throw new UnsupportedOperationException("empty.myMinBy")
t.reduceLeft((x, y) => if (cmp.lteq(f(x), f(y))) x else y)
}
}
}
방법이 테스트를 오는 :
이 정의를 감안할 때?
error: No implicit Ordering defined for AnyVal{def getClass(): java.lang.Class[_ >: Int with Double <: AnyVal]}. val mx = List(1, 2, 7, 1, 4, 8, 2, 5, 47, 2, 7).myMinBy(_.toDouble)
나는 스칼라 2.9.1을 사용하고 있습니다 – eirirlar