2015-02-07 54 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) 

或許在另外一個問題,你可以說你正在嘗試做的,詢問如何做到這一點:

我們可以從左至右由圖形描繪的繼承層次顯示這個?

相關問題