Skip to content

Instantly share code, notes, and snippets.

@durban
Last active November 22, 2016 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durban/8c2ffdc426b89fd379ea9699f63464e8 to your computer and use it in GitHub Desktop.
Save durban/8c2ffdc426b89fd379ea9699f63464e8 to your computer and use it in GitHub Desktop.
Scala 2.12 transient lazy val
lazy val core = project.in(file("."))
.settings(commonSettings)
lazy val commonSettings = Seq[Setting[_]](
scalaVersion := "2.11.8",
crossScalaVersions := Seq("2.11.8", "2.12.0"),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
"-unchecked",
"-encoding", "UTF-8",
"-Xlint:_",
"-Xfuture"
)
)
> ++ 2.11.8
[info] Setting version to 2.11.8
[info] Reapplying settings...
[info] Set current project to core (in build file:/home/.../transient/)
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> :javap TestClass -p
Compiled from "test.scala"
public class TestClass implements scala.Serializable {
private transient MyClass foo;
private volatile transient boolean bitmap$trans$0;
private MyClass foo$lzycompute();
public MyClass foo();
public TestClass();
}
scala> :quit
[success] Total time: 8 s, completed Nov 22, 2016 8:47:04 PM
> ++ 2.12.0
[info] Setting version to 2.12.0
[info] Reapplying settings...
[info] Set current project to core (in build file:/home/.../transient/)
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.0 (OpenJDK 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> :javap TestClass -p
Compiled from "test.scala"
public class TestClass implements scala.Serializable {
private MyClass foo;
private volatile boolean bitmap$0;
private MyClass foo$lzycompute();
public MyClass foo();
public TestClass();
}
scala> :quit
[success] Total time: 17 s, completed Nov 22, 2016 8:47:30 PM
>
[info] Set current project to core (in build file:/home/.../transient/)
> run
[info] Running Main
MyClass@335f5fe6
[success] Total time: 0 s, completed Nov 22, 2016 8:41:56 PM
> ++ 2.12.0
[info] Setting version to 2.12.0
[info] Reapplying settings...
[info] Set current project to core (in build file:/home/.../transient/)
> run
[info] Running Main
MyClass@403b81fd
[error] (run-main-1) java.io.NotSerializableException: MyClass
[error] - field (class "TestClass", name: "foo", type: "class MyClass")
[error] - root object (class "TestClass", TestClass@7ceaba54)
java.io.NotSerializableException: MyClass
- field (class "TestClass", name: "foo", type: "class MyClass")
- root object (class "TestClass", TestClass@7ceaba54)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1182)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Main$.main(test.scala:17)
at Main.main(test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 0 s, completed Nov 22, 2016 8:42:01 PM
>
class MyClass // not Serializable
class TestClass extends Serializable {
@transient
lazy val foo: MyClass =
new MyClass
}
object Main {
def main(args: Array[String]): Unit = {
val obj = new TestClass
println(obj.foo)
val os = new java.io.ObjectOutputStream(new java.io.ByteArrayOutputStream)
os.writeObject(obj)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment