vossad01
vossad01

Reputation: 11948

Collections code involving mutable.IndexedSeq, view, take, and grouped throws ClassCastException

The following scala code compiles fine.

object Main extends App {
  import scala.collection.mutable.IndexedSeq

  def doIt() {
    val nums: IndexedSeq[Int] = Array(3,5,9,11)
    val view: IndexedSeq[Int] = nums.view
    val half: IndexedSeq[Int] = view.take(2)
    val grouped: Iterator[IndexedSeq[Int]] = half.grouped(2)
    val firstPair: IndexedSeq[Int] = grouped.next() //throws exception here
  }

  doIt()
}

However, at runtime it thows java.lang.ClassCastException: scala.collection.SeqViewLike$$anon$1 cannot be cast to scala.collection.mutable.IndexedSeq on the call grouped.next()

I would expect the call to grouped.next() to return something equal to IndexedSeq[Int](3,5)

I am wondering why is this code failing, and if there a proper way to fix it?


If I repeat the same steps in the REPL, the type information confirms why the code compiles, but does not give me any insight to why the exception was thrown:

scala> val nums = Array(3,5,9,11)
nums: Array[Int] = Array(3, 5, 9, 11)

scala> val view = nums.view
view: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqView(...)

scala> val half = view.take(2)
half: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)

scala> val grouped = half.grouped(2)
grouped: Iterator[scala.collection.mutable.IndexedSeqView[Int,Array[Int]]] = non-empty iterator

scala> val firstPair = grouped.next()
java.lang.ClassCastException: scala.collection.SeqViewLike$$anon$1 cannot be cast to scala.collection.mutable.IndexedSeqView

Scala version 2.10.0-20121205-235900-18481cef9b -- Copyright 2002-2012, LAMP/EPFL

Upvotes: 2

Views: 396

Answers (1)

0__
0__

Reputation: 67280

Looks like you ran into bug SI-6709

Upvotes: 2

Related Questions