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



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

in scala 2.11.2, next minimal illustration compiles when using type ascription on array[string]:

object foo { def fromlist(list: list[string]): foo = new foo(list.toarray : array[string]) } class foo(source: indexedseq[string])

if remove type ascription in fromlist, fail compile next error:

error:(48, 56) polymorphic look cannot instantiated expected type; found : [b >: string]array[b] required: indexedseq[string] def fromlist(list: list[string]): foo = new foo(list.toarray) ^

why can't compiler infer array[string] here? or issue have implicit conversion array's indexedseq's?

the issue .toarray method returns array of type b superclass of t in list[t]. allows utilize list.toarray on list[bar] array[foo] required if bar extends foo.

yes, real reason doesn't work out of box compiler trying figure out b utilize , how indexedseq. seems it's trying resolve indexedseq[string] requirement b guaranteed string or superclass of string; hence error.

this preferred work around:

def fromlist(list: list[string]): foo = new foo(list.toarray[string])

arrays scala implicit-conversion type-inference scala-collections

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -