Ordering[A] を拡張するクラス A と A のサブクラス B を定義した後、B の配列を自動的にソートするにはどうすればよいでしょうか? Scala コンパイラーは、「パラメーター ord: Ordering[B] の暗黙的な値が見つかりませんでした」と文句を言います。A = Score および B = CommentedScore の具体的な REPL の例 (Scala 2.8) を次に示します。
class Score(val value: Double) extends Ordered[Score] {
def compare(that: Score) = value.compare(that.value)
}
defined class Score
trait Comment { def comment: String }
defined trait Comment
class CommentedScore(value: Double, val comment: String) extends Score(value) with Comment
defined class CommentedScore
val s = new CommentedScore(10,"great")
s: CommentedScore = CommentedScore@842f23
val t = new CommentedScore(0,"mediocre")
t: CommentedScore = CommentedScore@dc2bbe
val commentedScores = Array(s,t)
commentedScores: Array[CommentedScore] = Array(CommentedScore@b3f01d, CommentedScore@4f3c89)
util.Sorting.quickSort(commentedScores)
error: could not find implicit value for parameter ord: Ordering[CommentedScore]
util.Sorting.quickSort(commentedScores)
^
定型文を回避するエレガントな方法で、これを修正するにはどうすればよいですか (つまり、Array[B] = Array[CommentedScore] を「無料で」ソートします。Array[A] = Array[Score] をソートする方法を知っている場合)。 ?
ありがとう!