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

ClassCastException with structural types, value classes

    Details

      Description

      A structural type with a value class parameter throws a ClassCastException at runtime.

      object D {
        def main(args: Array[String]) {
          val a = new { def y[T](x: X[T]) = x.i }
          val x = new X(3)
          val t = a.y(x)
          println(t)
        }
      }
      class X[T](val i: T) extends AnyVal
      
      java.lang.ClassCastException: X cannot be cast to java.lang.Integer
      	at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
      	at D$.main(D.scala:5)
      	at D.main(D.scala)
      

        Issue Links

          Activity

          Hide
          Josh Suereth added a comment -

          This is now disallowed. Marking as resolved. Should I open a feature request to make this work in the future?

          Show
          Josh Suereth added a comment - This is now disallowed. Marking as resolved. Should I open a feature request to make this work in the future?
          Hide
          Mark Harrah added a comment -

          Probably needs to be disallowed in the return type as well:

          object D {
            def main(args: Array[String]) {
              val a = new { def y[T](x: T): X[T] = new X(x) }
              val t = a.y(3).i + 1
              println(t)
            }
          }
          
          class X[T](val i: T) extends AnyVal
          
          java.lang.ClassCastException: java.lang.Integer cannot be cast to X
             at D$.main(D.scala:4)
             at D.main(D.scala)
          
          Show
          Mark Harrah added a comment - Probably needs to be disallowed in the return type as well: object D { def main(args: Array[String]) { val a = new { def y[T](x: T): X[T] = new X(x) } val t = a.y(3).i + 1 println(t) } } class X[T](val i: T) extends AnyVal java.lang.ClassCastException: java.lang.Integer cannot be cast to X at D$.main(D.scala:4) at D.main(D.scala)
          Hide
          Jason Zaugg added a comment - - edited

          One more to test (I'll check it tonight if noone beats me to it)

          scala> class X[T](val i: T) extends AnyVal
          defined class X
          
          scala> (new { def y[T, XT <: X[T]](xt: XT) = xt.i }).y[Int, X[Int]](new X(0))
          java.lang.VerifyError: (class: , method: <init> signature: ()V) Expecting to find object/array on stack
          

          UPDATE:

          This example work fine. The error I was seeing was from an older milestone, M5.

          Show
          Jason Zaugg added a comment - - edited One more to test (I'll check it tonight if noone beats me to it) scala> class X[T](val i: T) extends AnyVal defined class X scala> (new { def y[T, XT <: X[T]](xt: XT) = xt.i }).y[Int, X[Int]](new X(0)) java.lang.VerifyError: (class: , method: <init> signature: ()V) Expecting to find object/array on stack UPDATE: This example work fine. The error I was seeing was from an older milestone, M5.
          Hide
          Josh Suereth added a comment -

          Look at the comments, we still have a missing case issue.

          Show
          Josh Suereth added a comment - Look at the comments, we still have a missing case issue.
          Hide
          Martin Odersky added a comment -
          Show
          Martin Odersky added a comment - New pull request: https://github.com/scala/scala/pull/1342

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development