widening/inference of foo[T](T,T): T

Assume there are three functions:

def foo[T](a:T, b:T): T = a 
def test1 = foo(1, "2") 
def test2 = foo(List(), ListBuffer()) 

While test1 is of type Any, test2 does not compile. Why is that? Both List() and ListBuffer() are of type Any, so why is test2 is not of type Any as well? Also both of them are of type SeqFactory, so can Scala somehow infer that type of test2 is SeqFactory?

foo(ListBuffer(), "") and foo(List(), "") work as expected


Looks like a bug to me. Scala first infers Seq[Nothing]{def seq: Seq[Nothing]{def companion: scala.collection.generic.GenericCompanion[Seq[Any]]}; def companion: scala.collection.generic.GenericCompanion[Seq[Any]]} Seq[Nothing]{def seq: Seq[Nothing]{def companion: scala.collection.generic.GenericCompanion[Seq[Any]]}; def companion: scala.collection.generic.GenericCompanion[Seq[Any]]} , and then decides ListBuffer[Nothing] doesn't really fit that type.

链接地址: http://www.djcxy.com/p/6484.html

上一篇: 为MySQL表生成数据

下一篇: foo [T](T,T)的拓宽/推理:T