I want to translate the following piece of code from Java to Scala:
Foo foo = new Foo() { private static final long serialVersionUID = 12345L; }
Class Foo
is an abstract class.
How does the equivalent code look like in Scala?
I want to translate the following piece of code from Java to Scala:
Foo foo = new Foo() { private static final long serialVersionUID = 12345L; }
Class Foo
is an abstract class.
How does the equivalent code look like in Scala?
There is a Scala annotation for adding the ID. But it seems, that you cannot apply this solution to anonymous inner classes. However, according to the Scala FAQ:
In Scala private values that evaluate to a constant known at compile-time are turned into private static final Java variables. This undocumented feature should do the trick for you. Just check out the implementation of lists in Scala (see e.g. src/scala/List.java). Both classes :: and Nil have a field serialVersionUID of the following form: private val serialVersionUID = numeric literal;
The code
object Ser extends Application {
trait Foo { def xy: Int }
val x = new Foo with java.io.Serializable { def xy = 2; private val serialVersionUID = 1L }
}
compiles fine with the 2.8.1 compiler. I haven't tested it, though as to whether the serial version of the resulting class is actually the one supplied.