斯卡拉文档说Enumeration.Val是有序的,但我得到不一致的行为的时候,当我试图强制执行枚举值类型限制,要求他们支持排序:未定购Scala枚举值?
object Dogs extends Enumeration {
val Sam, Tom, Rover = Value
}
def doSomething[A <% Ordered[A]](a : List[A]) : Unit = {
println(a.sortWith(_ < _))
}
import Dogs._
val xs = List(Rover, Tom, Sam, Sam, Rover)
println(xs.sortWith(_ < _)) // works!
doSomething(xs) // fails =(
最后两条语句,第一工作并显示Enumeration值具有已定义的顺序。第二个给出错误:
could not find implicit value for evidence parameter of type (this.Dogs.Value) => Ordered[this.Dogs.Value]
如何解决此问题并在需要排序的泛型方法中使用枚举值?
谢谢@retronym,这最后一个例子解决了我的问题,虽然我的思想是从较弱的类型限制弯曲! 现在需要任何类型A,只要A可以被当作(即,或可以被转换成)某种东西。 – 2010-03-02 22:30:24