我需要定义一些案例类,如下所示:case class Gt(key: String, value: Any) extends Expression { def evalute[V, E](f: String => Any) = { def compare(v: Any): Boolean = { v match { case x: Number => x.doubleValue > value.asInstanceOf[Number].doubleValue case x: Array[_] => x.forall(a => compare(a)) case x => x.toString > value.toString } } compare(f(key)) }}我不喜欢对> =和我也试过这个:trait Expression { def evalute[V, E](f: String => Any) = true def compare(v: Any, value: Any, cp: (Ordered[_], Ordered[_]) => Boolean): Boolean = { v match { case x: Number => cp(x.doubleValue, value.asInstanceOf[Number].doubleValue) case x: Array[_] => x.forall(a => compare(a, value, cp)) case x => cp(x.toString, value.toString) } }}case class Gt(key: String, value: Any) extends Expression { def evalute[V, E](f: String => Any) = { compare(f(key), value, ((a, b) => a > b)) }}但这不起作用:(error: could not find implicit value for parameter ord: scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]compare(f(key), value, ((a, b) => a > b))有没有一种方法可以将运算符作为scala中的函数传递? 最佳答案 (a, b) => a > b正常工作。您的问题在于类型。 V中的E和evalute[V, E]应该是什么? 您将(a, b) => a > b传递为参数cp: (Ordered[_], Ordered[_]) => Boolean。所以你有a: Ordered[_]和b: Ordered[_]。与a: Ordered[X] forSome {type X}和b: Ordered[Y] forSome {type Y}相同。对于这些类型,a > b没有任何意义。关于scala - 如何使用> <<=> =作为函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4048713/