Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is no bug. <code>Array</code> is not covariant. B being a subtype of B does not make Array[B] a subtype of Array[A]. This is contrary to java, where B[] is a subtype of A[], which is unsound:</p> <pre><code>A[] b = new B[1]; b[0] = new (A); -&gt; ArrayStoreException </code></pre> <p>So your Array[Some[Any]] is not an Array[Option[Any]]. You must make sure you have an Array[Option], which you may do with</p> <pre><code>val row2 = Array[Option[Any]](Some(test2), Some(12345)) </code></pre> <p>You can also use a type abscription on one of the item:</p> <pre><code>val row2 = Array(Some(test2): Option[String], Some(12345)) </code></pre> <p>Or if you know your values are non-null, </p> <pre><code>val row2 = Array(Option(test2), Option(12345)) </code></pre> <p>(It's enough to do that on one of the value)</p> <p><code>List</code>, on the other hand is covariant, which is why it works. </p> <p>It is actually unfortunate that the more precise type <code>Some</code> is inferred, it is quite uncommon that you want the type of something to be known as <code>Some</code> (or <code>None</code>) rather than <code>Option</code>.</p> <p><strong>Edit</strong> Sorry, looks like I completely missed the point, about why there are different. <code>Array</code> is not covariant, but Iterable is. So it would seems that <code>Array[B]</code> while not being an <code>Array[A]</code>, should be an <code>Iterable[A]</code>, then everything should work. It would be so if Array was a subtype of Iterable. It is not, it comes with the JVM, cannot be made to extend <code>Iterable</code>. What there is is an implicit conversion to <code>WrappedArray</code>, which is an <code>Iterable</code>. </p> <p>When you write <code>val l = List(row1, row2)</code>, it has no reason to apply this conversion. It types list as precisely as it can. Then the fact that List is covariant (a List[B] is a List[A] if B is an A) will not kick in when we have not B is an A, but B has an implicit conversion to A. </p> <p>On the other hand, when you write val l: List[Iterable[A]] = List(x,y) then the List(...) function expects Iterable[A] arguments, and at this point it looks for implicit conversions. </p> <p>Still not a bug, but trickier than I thought. Maybe you could do a </p> <pre><code>class Container[T &lt;% Iterable[Option[Any]]](val rows: Iterable[T]) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload