3

ページのDotty下のドキュメントを見ていたら、 .Contextual AbstractionsGiven Instances

与えられたインスタンス (または、単に「与えられた」) は、与えられた句への引数を合成するために役立つ特定の型の「標準的な」値を定義します。例:

trait Ord[T] {
  def compare(x: T, y: T): Int
  def (x: T) < (y: T) = compare(x, y) < 0
  def (x: T) > (y: T) = compare(x, y) > 0
}

given intOrd: Ord[Int] {
  def compare(x: Int, y: Int) =
    if (x < y) -1 else if (x > y) +1 else 0
}

given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] {

  def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match {
    case (Nil, Nil) => 0
    case (Nil, _) => -1
    case (_, Nil) => +1
    case (x :: xs1, y :: ys1) =>
      val fst = ord.compare(x, y)
      if (fst != 0) fst else compare(xs1, ys1)
  }
}

しかし、ドキュメントのこの例では、 の使用方法は説明されていませんgiven。テストサンプルプロジェクトをプルしてDotty使ってみたのですが、よくわかりません。

新しいキーワードですか?輸入しますか?または、何か不足していますか。

4

3 に答える 3