- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Problem in the compiler that break serialization: closures in inizializer of structural types
Tue, 2011-10-25, 05:35
In working on my scala layer on top of hadoop I have encontered the following issue with the code generated by the compiler that breaks serialization. It is not a big deal normally, but it is a very annoying and difficult problem to track, and if solved might help also the folks developing spark, which I heard ran into similar issues.
import java.io.ObjectOutputStream
import java.io.ByteArrayOutputStream
object SerializerBug {
def main(args: Array[String]): Unit = {
val a = 1
val b = "hello, world"
val obj = new {
val f = () => println(a.toString + " " + b)
}
obj.f()
new ObjectOutputStream(new ByteArrayOutputStream).writeObject(obj)
}
}
breaks with the following output:
1 hello, world
Exception in thread "main" java.io.NotSerializableException: SerializerBug$$anon$1
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at SerializerBug$.main(SerializerBug.scala:14)
at SerializerBug.main(SerializerBug.scala)
I added it to jira: