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]]}
, and then decides ListBuffer[Nothing]
doesn't really fit that type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With