Skip to content

closures in initializer of structural types break serialization #5048

Open
@scabug

Description

@scabug

This program

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions