Scala Programming Language
  1. Scala Programming Language
  2. SI-6337

ClassCastException from nested, parameterized value classes

    Details

      Description

      Getting the value from nested, parameterized value classes throws ClassCastException at runtime.

      object C {
        def main(args: Array[String]) {
          val x = new X(new XX(3))
          println(x.i.x + 9)
        }
      }
      
      class X[T](val i: XX[T]) extends AnyVal
      class XX[T](val x: T) extends AnyVal
      
      $ scalac C.scala
      $ scala C
      java.lang.ClassCastException: XX cannot be cast to java.lang.Integer
      	at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
      	at C$.main(C.scala:4)
      	at C.main(C.scala)
      

        Issue Links

          Activity

          Hide
          Josh Suereth added a comment -

          Martin - I'm bumping this to you, send back if you need.

          Show
          Josh Suereth added a comment - Martin - I'm bumping this to you, send back if you need.
          Hide
          Mark Harrah added a comment -

          Note that this doesn't require a bare type parameter in XX.

          object C {
            def main(args: Array[String]) {
              val x = new X(new XX(Some(3)))
              println(x.i.x.get + 9)
            }
          }
          
          class X[T](val i: XX[T]) extends AnyVal
          class XX[T](val x: Option[T]) extends AnyVal
          

          The error is:

          java.lang.ClassCastException: XX cannot be cast to scala.Option
          
          Show
          Mark Harrah added a comment - Note that this doesn't require a bare type parameter in XX. object C { def main(args: Array[String]) { val x = new X(new XX(Some(3))) println(x.i.x.get + 9) } } class X[T](val i: XX[T]) extends AnyVal class XX[T](val x: Option[T]) extends AnyVal The error is: java.lang.ClassCastException: XX cannot be cast to scala.Option
          Hide
          Mark Harrah added a comment -

          A similar problem occurs with a nested universal trait:

          object C {
            def main(args: Array[String]) {
              val x = X(XX(3))
              println(x.q.x.x + 9)
            }
          }
          trait Q extends Any {
             def x: Int
             def inc: XX
          }
          case class X(val x: Q) extends AnyVal {
             def q = X(x.inc)
          }
          case class XX(val x: Int) extends AnyVal with Q {
             def inc = XX(x + 1)
          }
          
          java.lang.ClassCastException: XX cannot be cast to java.lang.Integer
          	at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
          	at XX.inc(XX.scala:15)
          	at X$.extension$q(XX.scala:13)
          	at C$.main(XX.scala:4)
          	at C.main(XX.scala)
          
          Show
          Mark Harrah added a comment - A similar problem occurs with a nested universal trait: object C { def main(args: Array[String]) { val x = X(XX(3)) println(x.q.x.x + 9) } } trait Q extends Any { def x: Int def inc: XX } case class X(val x: Q) extends AnyVal { def q = X(x.inc) } case class XX(val x: Int) extends AnyVal with Q { def inc = XX(x + 1) } java.lang.ClassCastException: XX cannot be cast to java.lang.Integer at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source) at XX.inc(XX.scala:15) at X$.extension$q(XX.scala:13) at C$.main(XX.scala:4) at C.main(XX.scala)
          Show
          Martin Odersky added a comment - https://github.com/scala/scala/pull/1325
          Hide
          Mark Harrah added a comment -

          Nested value classes weren't required for this latest problem. See SI-6385.

          Show
          Mark Harrah added a comment - Nested value classes weren't required for this latest problem. See SI-6385 .

            People

            • Assignee:
              Martin Odersky
              Reporter:
              Mark Harrah
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development