【发布时间】:2010-11-16 19:00:21
【问题描述】:
通过混合 Ordered 并提供 compare() 函数,对对象进行排序非常简单,如 here 所示。但是,如果您的排序值是 Double 而不是 Int 怎么办?
def compare(that: MyClass) = this.x - that.x
其中 x 是 Double 将导致编译器错误:“type mismatch; found: Double required: Int”
有没有办法使用 Doubles 进行比较而不是转换为 Ints?
【问题讨论】:
-
这个成语对于整数类型也被破坏了,因为它会在溢出时产生错误的结果。
标签: scala comparison