-
Notifications
You must be signed in to change notification settings - Fork 21
ClassCastException on calling vararg ctor #7436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-7436?orig=1 |
@paulp said: |
Stave Escura (stave.escura-at-gmail.com) said: In Java I need varargs in order to make any thing even slightly approaching a DSL (mostly in tests). I don't know if the need is as great in Scala. The example in the link shows what I think is a sensible use of varargs. I have an alternative implementation using Lists, but the code is much more verbose and not nearly as readable. I'd really appreciate some idiomatically correct code that did the same thing as the example not using val p:Int* |
@sschaef said: |
@retronym said: |
@Sciss said: Welcome to Scala version 2.10.3-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
scala> class A(val p: Int*); case class B(p1: Int) extends A(p1); new B(1)
java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofInt cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:106)
at B.p1(<console>:18)
... |
@Sciss said (edited on Sep 19, 2013 10:36:07 PM UTC): Welcome to Scala version 2.10.3-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class A(val p: Int*); case class B(p1: Int) extends A(p1); new B(1)
java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofInt cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:106)
at B.p1(<console>:7)
at ... |
@sschaef said: |
@retronym said: qbin/scala
Welcome to Scala version 2.11.0-20130919-080201-884e1ce762 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class A(val p: Int*); case class B(p1: Int) extends A(p1); new B(1)
defined class A
defined class B
res0: B = B(1) |
Adam Retter (adamretter) said: My test case is not as elegant as the one initially posted, but for completeness, here it is: object Blah extends App {
trait ArgProvider {
}
case class ColumnReference(value: String) extends ArgProvider
abstract class Rule(name: String, val argProviders: ArgProvider*)
case class InRule(inValue: ArgProvider) extends Rule("in", inValue) {
def valid(): Boolean = {
println(inValue.getClass) //This causes a ClassCastException on 2.10.1+ but not 2.10.0
true
}
}
val rule = InRule(ColumnReference("Column1"))
rule.valid()
} I have been suggested the workaround of defining the case class InRule(inValue: ArgProvider) extends Rule("in", Seq(inValue): _*) {
...
} Whilst the workaround helps me, I still have to modify my code to move onward from 2.10.0 to 2.10.1+ which is not ideal. |
Adam Retter (adamretter) said: It is nice to know it is fixed in the upcoming 2.11, but not everyone has the luxury of being able to easily move to 2.11, especially when there is not yet a stable version. |
@adriaanm said: |
@adriaanm said: |
Adam Retter (adamretter) said: |
@Sciss said: |
@adriaanm said: |
This code fails at runtime:
The error only occurs in combination of a case class extending a class taking a vararg ctor that is marked as val.
Looking at the generated code the bug is obvious:
and
p1 is not correctly set in the ctor of B. Instead it treats p as the same value and tries to get the value from it.
The text was updated successfully, but these errors were encountered: