Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala type inference: can't infer IndexedSeq[T] from Array[T]

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?

like image 842
Chris Avatar asked Oct 31 '14 15:10

Chris


1 Answers

The issue is that the .toArray method returns an Array of some type B which is a superclass of the T in List[T]. This allows you to use a list.toArray on List[Bar] where an Array[Foo] is required if Bar extends Foo.

Yes, the real reason this doesn't work out of the box is that the compiler is trying to figure out which B to use and also how to get to an IndexedSeq. It seems like it's trying to resolve the IndexedSeq[String] requirement but B is only guaranteed to be a String or a superclass of String; hence the error.

This is my preferred work around:

def fromList(list: List[String]): Foo = new Foo(list.toArray[String])
like image 200
Nate Avatar answered Oct 16 '22 04:10

Nate