This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How can I specify the "serialVersionUID" static field when serializing objects in Scala?

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>;

You can use any value for the numeric literal or you can do it the Java way and use serialver for that purpose. If you want to add such an ID to a class or trait named mypackage.Foo, you would call serialver in the following way:

> serialver mypackage.Foo$class

For a top-level object mypackage.Obj you write:

> serialver mypackage.Obj$

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland