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

Problem in the compiler that break serialization: closures in inizializer of structural types

No replies
alexcozzi
Joined: 2011-04-05,
User offline. Last seen 1 year 27 weeks ago.
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:

https://issues.scala-lang.org/browse/SI-5048

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