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
Post a Comment