Last active
November 22, 2016 19:48
-
-
Save durban/8c2ffdc426b89fd379ea9699f63464e8 to your computer and use it in GitHub Desktop.
Scala 2.12 transient lazy val
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> ++ 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 | |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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