I am new to Scala. I went through a couple of books and read some online lessons. My first project has problems, so I reduced the code to the simplest thing that might go wrong.
I searched google and stack overflow for scala / constructors / varargs and read a couple of scala tours.
The simplest code (almost):
class Foo(val params: Int*) case class Foo1(val p: Int) extends Foo(p) case class Foo2(val p1: Int, val p2: Int) extends Foo(p1, p2) object Demo extends App { override def main(args: Array[String]) { val f = Foo2(1, 2) f.p1 } }
An exception occurs when accessing p1 and
Exception in thread "main" java.lang.ClassCastException: scala.collection.mutable.WrappedArray $ ofInt cannot be added to java.lang.Integer
Resorting to debugging using eclipse, I found an interesting property: when viewing variables
f Foo2 (id=23) p2 2 params WrappedArray$ofInt (id=33) array (id=81) [0] 1 [1] 2
So what happened to p1?
I apologize for the fact that you were worried about the newcomer.
source share