How can collections use implicit conversions on element types?

前端 未结 2 769
误落风尘
误落风尘 2021-01-14 00:32

While working on this question, I came up with the following issue. Consider two method definitions:

def foo[T <: Ordered[T]](s : Seq[T]) = s.sorted

def          


        
相关标签:
2条回答
  • 2021-01-14 00:45
    scala> implicit def ordering[T <% Ordered[T]] = new Ordering[T]{def compare(x: T, y: T) = x compare y}
    ordering: [T](implicit evidence$1: (T) => Ordered[T])java.lang.Object with Ordering[T]
    
    scala> def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
    foo: [T](s: Seq[T])(implicit evidence$1: (T) => Ordered[T])Seq[T]
    
    0 讨论(0)
  • 2021-01-14 00:52
    % scala29
    Welcome to Scala version 2.9.0.r24168-b20110202012927 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
    foo: [T](s: Seq[T])(implicit evidence$1: (T) => Ordered[T])Seq[T]
    
    scala>
    

    By the way, re "here it seems easy", it wasn't. Implicits like these enjoy diverging and they were pretty determined.

    0 讨论(0)
提交回复
热议问题