Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-4190

mutable.IndexedSeq.view.map returns collection.SeqView

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Scala 2.13.0-M1
    • Component/s: None
    • Labels:
      None

      Description

      === What steps will reproduce the problem (please be specific and use wikiformatting)? ===

      import collection.mutable._
       
      val x: ArrayBuffer[String] = ArrayBuffer("a", "b", "c")
      val y: IndexedSeq[String] = x.view map (_ + "0")
      

      This code compiles but produces a `ClassCastException` in the last line:

      java.lang.ClassCastException: scala.collection.SeqViewLike$$$$anon$$3 cannot be cast to scala.collection.mutable.IndexedSeq
      

      === What is the expected behavior? ===
      A compiler error or working code.

      === What versions of the following are you using? ===

      • Scala: 2.8.1

        Attachments

          Issue Links

            Activity

            Hide
            moors Adriaan Moors added a comment -

            Since 2.11.0-RC1 is one week away, pushing all non-blockers without PR to 2.11.1-RC1. Please undo the change if I missed work in progress.

            Show
            moors Adriaan Moors added a comment - Since 2.11.0-RC1 is one week away, pushing all non-blockers without PR to 2.11.1-RC1. Please undo the change if I missed work in progress.
            Hide
            ichoran Rex Kerr added a comment -

            This can't be fixed without breaking binary compatibility. Changing fix version to 2.12.

            Show
            ichoran Rex Kerr added a comment - This can't be fixed without breaking binary compatibility. Changing fix version to 2.12.
            Hide
            ichoran Rex Kerr added a comment -

            More related brokenness reported by Dima Golubets:

            scala> Array(1, 2, 3, 4).view
            res60: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqView(...)
            OK
             
            scala> Array(1, 2, 3, 4).view.map{x => println(x); x}
            res61: scala.collection.SeqView[Int,Array[Int]] = SeqViewM(...)
            OK
             
            scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x}
            res62: Seq[Int] = SeqViewMM(...)
            WTF? Why Seq?
             
            After that point I can't call a force method. But I want to get some results so lets try:
             
            scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x}.take(1).toList
            1
            1
            res64: List[Int] = List(1)
            OK..
             
            scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x}.take(1).toArray
            1
            1
            1
            1
            res65: Array[Int] = Array(1)
            Double view evaluation!
            

            Note that double view evaluation isn't forbidden by the contract, but views should perhaps be a little smarter about the choice of algorithm

            Show
            ichoran Rex Kerr added a comment - More related brokenness reported by Dima Golubets: scala> Array(1, 2, 3, 4).view res60: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqView(...) OK   scala> Array(1, 2, 3, 4).view.map{x => println(x); x} res61: scala.collection.SeqView[Int,Array[Int]] = SeqViewM(...) OK   scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x} res62: Seq[Int] = SeqViewMM(...) WTF? Why Seq?   After that point I can't call a force method. But I want to get some results so lets try:   scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x}.take(1).toList 1 1 res64: List[Int] = List(1) OK..   scala> Array(1, 2, 3, 4).view.map{x => println(x); x}.map{x => println(x); x}.take(1).toArray 1 1 1 1 res65: Array[Int] = Array(1) Double view evaluation! Note that double view evaluation isn't forbidden by the contract, but views should perhaps be a little smarter about the choice of algorithm
            Hide
            archeg Iurii Gazin added a comment -

            Not sure whether this is a separate bug or not, but it could be related:
            I'm trying to work with a string as a view. From my perspective this should return SeqView:

            "abc".view.updated(0, 'A')
            

            But it returns Seq, the same as in previous comment. It looks like it's missing appropriate CanBuildFrom.

            Seq('a', 'b', 'c').view.updated(0, 'A')
            

            works fine.

            It looks very similar to some of the code reported above, but this time it is about immutable structure.

            Scala 2.11.2

            Show
            archeg Iurii Gazin added a comment - Not sure whether this is a separate bug or not, but it could be related: I'm trying to work with a string as a view. From my perspective this should return SeqView: "abc".view.updated(0, 'A') But it returns Seq, the same as in previous comment. It looks like it's missing appropriate CanBuildFrom. Seq('a', 'b', 'c').view.updated(0, 'A') works fine. It looks very similar to some of the code reported above, but this time it is about immutable structure. Scala 2.11.2
            Hide
            ichoran Rex Kerr added a comment -

            I am not sure there is a simple fix of the sort that should go in for RC1. To fix this (without exposing a bunch of other equivalent holes) I had to rewrite a lot of the views library to make it differently-consistent. The problem is that the function given to `map` is not invertible, so you can't very well change the original values. But it's also confusing that some methods detach you from the original collection while others do not.

            I recommend punting until 2.13, Adriaan Moors Stefan Zeiger (at which point I may have time to get my tentative fixes in).

            Show
            ichoran Rex Kerr added a comment - I am not sure there is a simple fix of the sort that should go in for RC1. To fix this (without exposing a bunch of other equivalent holes) I had to rewrite a lot of the views library to make it differently-consistent. The problem is that the function given to `map` is not invertible, so you can't very well change the original values. But it's also confusing that some methods detach you from the original collection while others do not. I recommend punting until 2.13, Adriaan Moors Stefan Zeiger (at which point I may have time to get my tentative fixes in).

              People

              • Assignee:
                ichoran Rex Kerr
                Reporter:
                jrudolph Johannes Rudolph
                TracCC:
                Ismael Juma
              • Votes:
                0 Vote for this issue
                Watchers:
                11 Start watching this issue

                Dates

                • Created:
                  Updated: