Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use class wrappers instead of objects in the REPL for better serialization ? #5135

Open
smarter opened this issue Sep 21, 2018 · 9 comments
Open

Comments

@smarter
Copy link
Member

smarter commented Sep 21, 2018

As suggested by @alexarchambault, see com-lihaoyi/Ammonite#736.
Scala 2 also has -Yrepl-class-based to accomplish something similar but it's not the default. @adriaanm are there known issues with -Yrepl-class-based that prevent it from being the default ?

@adriaanm
Copy link
Contributor

One corner case pointed out in https://issues.scala-lang.org/browse/SI-7747 is that you can't define nested value classes.

@adriaanm
Copy link
Contributor

Other than that, it's mostly inertia that's keeping the default in place AFAIK.

@smarter
Copy link
Member Author

smarter commented Sep 21, 2018

@alexarchambault How does the ammonite repl handle value classes ?

@alexarchambault
Copy link
Contributor

It just doesn't handle them as is when class-wrapping is enabled (which is not the default).

Users can still force wrapping stuff in singletons if the code consists of a singleton too. Like

@ case class Foo(s: String) extends AnyVal

fails, but

@ object Defs {
    case class Foo(s: String) extends AnyVal
  }

is ok, as the latter will itself be wrapped in a singleton.

@smarter
Copy link
Member Author

smarter commented Sep 21, 2018

maybe we just need class-wrapping to be turned off when the expression defined contains a value class.

@alexarchambault
Copy link
Contributor

alexarchambault commented Sep 21, 2018

Just discussed with @allanrenucci: one can't define macros if everything is wrapped in classes (in scala 2.x at least, AFAIK). Hence the special-case for singletons above.

@smarter
Copy link
Member Author

smarter commented Jan 25, 2019

To recap (and let me know if I've left out something), the issue with object wrappers is that deserializing them does not actually deserialize the value of their fields, it just returns the singleton instance of the object class. This is problematic when computing these fields are expensive, and serialized instances are sent over the wire (e.g., Spark). Class wrappers solve this issue but introduce their own problems which don't seem trivial to work around.

So how about simply making object wrappers do what we want ? I see two ways to achieve this:

  • Change the serialization scheme for all objects to always serialize all their fields, and on deserialization set the fields which are still uninitialized in the object.
  • Or, if this is too radical, add an @alwaysSerialize annotation (the reverse of @transient), serializing an object should only serialize the fields in that object with that annotation. The REPL could automatically add this annotation to all vals declared in it.

/cc @retronym who's worked on serialization for objects recently

@smarter

This comment has been minimized.

@alexarchambault

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants