2013-04-03 61 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

谢谢,但你甚至读过这个问题吗? –