2012-03-13 28 views

回答

12

没有,因为绑定的情况下实际上是一个额外的隐含参数的简写。

例如:

def sort[A : Ordering](xs: Seq[A]) 

def sort[A](xs: Seq[A])(implicit ordering: Ordering[A]) 

的简写形式,这不能在类型定义来表示。

+0

感谢。这是有道理的。 – 2012-03-14 07:31:37

12

不必在类型声明中直接绑定上下文,而必须有一个单独的值声明来表示JPP提到的隐式参数。

谁定义的类型也将提供绑定的语境证据:

trait Generic { 
    type U 
    implicit val ordering: Ordering[U] // evidence for U: Ordering 

    def max(u1: U, u2: U) = List(u1, u2).max 
} 

def concrete[T: Ordering] = new Generic { 
    type U = T 
    val ordering = implicitly[Ordering[T]] 
} 

assert(concrete[Int].max(1,3) == 3) 
+2

这应该是被接受的答案 – 2016-04-22 11:57:56

相关问题