score:13
This is no bug. Array
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:
A[] b = new B[1];
b[0] = new (A);
-> ArrayStoreException
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
val row2 = Array[Option[Any]](Some(test2), Some(12345))
You can also use a type abscription on one of the item:
val row2 = Array(Some(test2): Option[String], Some(12345))
Or if you know your values are non-null,
val row2 = Array(Option(test2), Option(12345))
(It's enough to do that on one of the value)
List
, on the other hand is covariant, which is why it works.
It is actually unfortunate that the more precise type Some
is inferred, it is quite uncommon that you want the type of something to be known as Some
(or None
) rather than Option
.
Edit Sorry, looks like I completely missed the point, about why there are different. Array
is not covariant, but Iterable is. So it would seems that Array[B]
while not being an Array[A]
, should be an Iterable[A]
, 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 Iterable
. What there is is an implicit conversion to WrappedArray
, which is an Iterable
.
When you write val l = List(row1, row2)
, 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.
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.
Still not a bug, but trickier than I thought. Maybe you could do a
class Container[T <% Iterable[Option[Any]]](val rows: Iterable[T])
score:3
In first case, the type of listtest
is inferred from its definition: List[Array[Some[Any]]]
. @didierd explains why this is not a suitable type for the argument of Container
constructor.
In second case, the type argument of List(row1,row2)
is inferred from the fact it's used as the constructor argument.
score:3
Your problem is with implicit conversions. Scala does not try to apply implicit conversions for type parameters for 'contained types'.
Array is a not an Iterable, so if you were to use
val row1 = List(Some("test2"), Some(12345))
this works. But you're using Array, so it should try to find an implicit conversion for Array -> Iterable (which does exist), but it does not try. I'll try and find a better reference for this, but in the meantime, you can see this question.
If you explicitly specify as an Iterable the type of row, then this works, because the type inferencing works.
val row1: Iterable[Option[Any]] = Array(Some("test2"), Some(1234))
score:4
I tried your code and there was the same exception. Then I replaced
scala> val listtest = List(row1, row2)
on
scala> val listtest: Iterable[Iterable[Option[Any]]] = List(row1, row2)
listtest: Iterable[Iterable[Option[Any]]] = List(WrappedArray(Some(test), Some(1234)), WrappedArray(Some(test2), Some(12345)))
and it worked fine:
scala> val test = new Container(listtest)
test: Container = Container@a75974
Source: stackoverflow.com
Related Query
- Odd typing bug in Scala
- Iterate through odd members of collection in Scala
- Structured typing in Scala doesn't work with Double?
- Is this a bug in Scala 2.9.1 lazy implementation or just an artifact of decompilation
- Bug in Scala 2.10, Iterator.size?
- Scala Copy() Odd Behavior
- Avoid code duplication using duck typing in scala
- Scala duck typing pattern matching
- Is this a bug in Scala 2.10 String Interpolation inside a multiline String with backslash?
- Shuffling Range in Scala is Odd
- How to modify previous line in REPL - scala to modify the typing errors to save time compare to entering each line using up/down arrows
- Typing over companion object in Scala
- scala case class private apply method( repl bug ?)
- Scala bug or feature? Multiple assignment error with capital letter variables
- Odd NullPointerException when reproducing a Java OpenGL ES 2.0 demo in Scala
- Is this a bug in scala 2.9.0.1 actor library
- Scala bug with immutable Map in concurrent program?
- Possible bug with Scala 2.10.2 implicits
- Is there a way to avoid typing whole package names in the Scala interpreter in Eclipse?
- Scala self type with dependent typing
- Scala shapeless typing Map[Symbol, String] with case classes
- Subclassing and typing in scala
- Weird Scala bug related to implicit conversions on default constructor parameters
- Is this a scala bug or am I doing something wrong
- Dependent Typing style Equality Proofs in Scala
- Scala Duck Typing Newbie
- Interesting Scala typing solution, doesn't work in 2.7.7?
- Typing Scala collections
- Odd Scala ClassCastException
- Scala object initialization bug
More Query from same tag
- Scala: Making concrete classes of trait with type member that has a representation type
- "when, thenReturn" not recognised
- Akka Streams: Calling push(_,_) not within onPull(_,_) is blocking the stream - Why?
- How to test if a `Some[String]` contains some other string?
- What is the reason for compilation errors if different version of Spark-core and Spark-mllib are mixed?
- Converting string to a collection of digits in Scala using map and toInt
- Casbah's problem with large number of returned objects
- Scala IDE (Eclipse) Run as Scala Application
- How to package jar to a given directory in sbt?
- How to process JSON documents (from MongoDB) and write to HBase in Structured Streaming?
- Declare self-type of a child trait in a parent trait in scala
- Using CustomBundle template in chisel inside a class that extends blackbox
- WS response body as bytes
- How to make two function parameters as implicit
- Changing XML Namespace with Scala
- java.lang.ClassNotFoundException: com.datastax.spark.connector.rdd.partitioner.CassandraPartition
- Scala - XML parsing tag not working properly
- Akka cluster and OpenShift
- Understanding breakdown of case classes
- Combining multiple structs into single struct in Spark SQL
- How to define generic type in Scala?
- Scala's type equivalent in Java
- How is pattern matching in Scala implemented at the bytecode level?
- GREMLIN for Scala : How to drop edge between two vertex and connect edges between two vertex in single query
- Jupyter Scala Kernel Missing
- Custom json serialization of structured scala case classes
- scala - bash: hw.scala: Permission denied
- How to count akkaHttp RejectionHandler rejections and successes?
- How to check errors cleanly in scala functionally?
- Liquibase cannot find parser