在作視圖界定的時候,但願比較兩個參數的"大小",代碼以下:java
class VBPair2[T <% Ordering[T]](val first:T,val second:T) { def bigger = if(first > second) first else second } object ViewBoundsTest { def main(args: Array[String]): Unit = { val pair2 = new VBPair2(2,3) println(pair2.bigger) } }
(上面代碼主要是想經過隱式轉換,將2,3轉換成RichInt再將RichInt隱式轉換成Ordered[T]/Ordering[T],從而具備>函數,進而比較大小)框架
發現提示有錯誤:less
Cannot resolve symbol >函數
也就是說Ordering[T]中沒有>函數,當我將Ordering[T]換成Ordered[T]則代碼成功。this
查看Ordered[T]的源代碼:spa
trait Ordered[A] extends Any with java.lang.Comparable[A] { /** Result of comparing `this` with operand `that`. * * Implement this method to determine how instances of A will be sorted. * * Returns `x` where: * * - `x < 0` when `this < that` * * - `x == 0` when `this == that` * * - `x > 0` when `this > that` * */ def compare(that: A): Int /** Returns true if `this` is less than `that` */ def < (that: A): Boolean = (this compare that) < 0 /** Returns true if `this` is greater than `that`. */ def > (that: A): Boolean = (this compare that) > 0 /** Returns true if `this` is less than or equal to `that`. */ def <= (that: A): Boolean = (this compare that) <= 0 /** Returns true if `this` is greater than or equal to `that`. */ def >= (that: A): Boolean = (this compare that) >= 0 /** Result of comparing `this` with operand `that`. */ def compareTo(that: A): Int = compare(that) } object Ordered { /** Lens from `Ordering[T]` to `Ordered[T]` */ implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]): Ordered[T] = new Ordered[T] { def compare(that: T): Int = ord.compare(x, that) } }
說明Ordered有一個伴生對象(我之前一直覺得只有class纔能有伴生object對象,如今經過scala源碼知道trait也能夠有伴生對象),而這個伴生對象中有一個隱式操做,implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]): Ordered[T],看到這個代碼,使我想起implicit在函數參數中使用時,不僅是對一個參數起做用,而是對全部參數都起做用,好比有下面一個函數:scala
def somfun(implicit arg1:Int,arg2:String,arg3:Double) 它說明不僅是arg1是隱式參數,arg2和arg3也是隱式參數。code
implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]): Ordered[T]
說明隱式轉換函數orderingToOrdered是一個柯里化函數,第二個參數是一個隱式參數,它的類型是Ordering[T],本例中調用的是int類型參數2,3,說明有一個隱式參數,類型爲Ordering[int],當調用的時候會找到Ordered伴生對象的orderingToOrdered隱式轉換函數,參數類型爲int,返回一個Ordered對象。Ordered類中有>函數,因此在本例中用Ordered則能夠正常運行。對象
scala的Ordering[T]代碼框架以下:接口
trait Ordering[T] extends Comparator[T] with PartialOrdering[T] with Serializable { 。。。。。 def compare(x: T, y: T): Int 。。。。 }
只有一個抽象方法compare。Ordering的父類Comparator(在ava包中)中也有一個抽象方法compare:
int compare(T o1, T o2);
也就是說:Scala提供兩個特質(trait)Ordered
與Ordering
用於比較。其中,Ordered混入(mix)Java的Comparable接口,而Ordering則混入Comparator接口。衆所周知,在Java中
Ordered與Ordering的區別與之相相似: