In Scala 2.11.2, the following minimal example compiles only when using type ascription on the Array[String]
:
object Foo {
def fromList(list: List[String]): Foo = new Foo(list.toArray : Array[String])
}
class Foo(source: IndexedSeq[String])
If I remove the type ascription in fromList
, it will fail to compile with the following error:
Error:(48, 56) polymorphic expression cannot be instantiated to expected type;
found : [B >: String]Array[B]
required: IndexedSeq[String]
def fromList(list: List[String]): Foo = new Foo(list.toArray)
^
Why can't the compiler infer the Array[String]
here? Or does this issue have to do something with the implicit conversion from Array
's to IndexedSeq
's?
object Foo { def fromList(list: List[String]): Foo = new Foo(list.toArray[String])}
instead. – Dave L.list.toIndexedSeq
, of course. The question's still a good one, though. – Travis BrownArray
s instead ofIndexedSeq
s is purely for performance reasons. I had to profile the function and found thatVector
s take more overhead when creating lots of small instances. – Chris