I am attempting to make a ScalaCheck matrix generator that will generate a 2D array/ matrix of a specified order (size/dim). I started with the example on the tutorial, and simplified it (for prototyping--I understand I should use sized to be able to resize future generators) to
def matrix[ T ]( g: Gen[ T ] )( order: Int ): Gen[ Array[ Seq[ T ] ] ] =
Gen.listOfN( order, Gen.listOfN( order, g ) ).map(_.toArray)
However, I could not get an Array[Array[T]] from this without putting in a ClassTag:
def matrix[ T : ClassTag]( g: Gen[ T ] )( order: Int ): Gen[ Array[ Seq[ T ] ] ] =
Gen.listOfN( order, Gen.listOfN( order, g ) ).map(_.toArray)
Which I sort-of understand--I'm not that experienced in Scala. In the process of getting there, I tried this:
def matrix2[ T : ClassTag]( g: Gen[ T ] )( order: Int ): Gen[ Array[ Array[ T ] ] ] =
for {
rowSeq <- Gen.listOfN( order, g )
rowArray <- rowSeq.toArray
seqOfRowArrays <- Gen.listOfN( order, rowArray)
matrix <- seqOfRowArrays.toArray
} yield matrix
but the compiler complained
"type mismatch; found : Array[T] required: org.scalacheck.Gen[Array[Array[T]]]"
on the rowArray line and
"Multiple markers at this line
- type mismatch; found : org.scalacheck.Gen[Array[T]] required: scala.collection.GenTraversableOnce[?]"
on the seqOfRowArrays line.
My questions are:
1. Is there a simpler way to get a defined size Array[Array[T]], ie some way to bound Arbitrary?
2. What is wrong with the typing of the above for expression?
3. When tests fail, how will I keep ScalaCheck from reducing the order during shrinking?