2013-04-03 99 views
0

比方說,我有一個包裝類類型推斷不一致

case class Cont [E] (e : Seq[E]) { 
    def :: [E1 >: E] (e1 : Seq[E1]) : Cont[E1] = Cont(e1 ++ e) 
    def + [E1 >: E] (e1 : Seq[E1]) : Cont[E1] = Cont(e1 ++ e) 
} 

而是對某種類型的序列的包裝。它可以接受另一種類型的序列,並返回新的包裝,用於附加序列現在使用它們超類型的類型。它有兩種方法 - 從右到左用::和從左到右用+。

現在這些鏈接的結果是:

Cont(Seq[Nothing]()) //-> Cont[Nothing] 
Seq[Nothing]() :: Cont(Seq[Nothing]()) //-> Cont[Nothing] 
Seq[Nothing]() :: Seq[Nothing]() :: Cont(Seq[Nothing]()) //-> Cont[E1] 
Seq[Int]() :: Seq[Nothing]() :: Seq[Nothing]() :: Cont(Seq[Nothing]()) //-> Cont[Any] 

Cont(Seq[Nothing]())//-> Cont[Nothing] 
Cont(Seq[Nothing]()) + Seq[Nothing]()//-> Cont[Nothing] 
Cont(Seq[Nothing]()) + Seq[Nothing]() + Seq[Nothing]() //-> Cont[Nothing] 
Cont(Seq[Nothing]()) + Seq[Nothing]() + Seq[Nothing]() + Seq[Int]() //-> Cont[Int] 

結果應該是相同的或不應該他們?他們不是。我需要第二個(從左到右)的行爲。我甚至不知道Cont [E1]是什麼意思。這有什麼原因?有沒有修復代碼使用::?

+1

這似乎是一個錯誤 - 「繼續[E1]」不是有效的類型。此外,對於第三種情況,「Cont [Nothing]」已足夠,對於第四種情況,「Cont [Int]」已足夠。 – sschaef

回答

0

您是否知道以中綴「類操作符」形式使用時以冒號結尾的方法名稱是右結合的並且是針對右側操作數的結果?

換句話說:

foo :: bar 

相當於:

bar.::(foo) 

而此:

foo + bar 

相當於

foo.+(bar) 

我相信這解釋了你在+::之間看到的差異。

+0

謝謝,但你甚至讀過這個問題嗎? –