2015-02-07 53 views
0
scala> trait Foo 
defined trait Foo 

scala> trait Bar[+V <: Foo] 
defined trait Bar 

scala> trait Baz[+V <: Foo] { 
    | def print[W >: V](bar: Bar[W]) = println("hello bar") 
    | } 
<console>:10: error: type arguments [W] do not conform to trait Bar's type parameter bounds [+V <: Foo] 
      def print[W >: V](bar: Bar[W]) = println("hello bar") 

scala> trait Baz[+V <: Foo] { 
    | def print[W >: V <: Foo](bar: Bar[W]) = println("hello bar") 
    | } 
defined trait Baz 

任何人都可以解释为什么[W >: V]不工作?以及如何使其工作?为什么最后一个案例正在起作用类型参数不符合特性Bar的类型参数边界

+0

这与您的[早期问题](http://stackoverflow.com/questions/28383284/type-arguments-w-do-not-conform-to-trait-type-parameter-bounds)有什么不同? – 2015-02-07 20:32:27

+0

阿尼希,是否为你解决了一些问题? – AmigoNico 2015-04-27 12:54:47

回答

1

Bar约束条件V的定义为Foo或其子类型。 W被限制为V超级类型。那么,有一些超类型V,它们的继承层次高于Foo,并且对于那些类型,约束Bar施加在V上没有被满足。

Any, AnyRef, Foo, SomeFooSubType, ... 
      V V    V, ... (V could be any of these) 
       ^(let's say it's this one, to illustrate) 
W, W,  W, W (then W could be any of these) 
^ ^ (but these two violate the constraint that Bar imposes) 

或许在另外一个问题,你可以说你正在尝试做的,询问如何做到这一点:

我们可以从左至右由图形描绘的继承层次显示这个?

相关问题