Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could an idiomatic design of Serializable/Cloneable/... look like in Scala?

I wonder how much different these funcionality would look like (and how different the implementation would be), if Scala wouldn't (have to) follow Java's java.io.Serializable/java.lang.Cloneable (mostly to stay compatible with Java and the tools/ecosystem around it).

Because Scala is more simpler in language design, but enables more powerful implementation and abstraction possibilities, it is thinkable that Scala might take a different path compared to Java, if it wouldn't have to shoulder the Java-compatibility-burden.

I could imagine that a idiomatic implementation would use type classes or traits with (possibly) private fields/methods (not possible in Java interfaces?), maybe carrying some standard implementation?

Or are marker interfaces still the right choice in Scala?

like image 849
soc Avatar asked Jun 11 '11 18:06

soc


2 Answers

Serialization and cloning are both kind of special because of mutability:

  • Serialization, because it has to deal with cycles in the object graph, and;
  • Cloning because... Well, the only reason to clone an object is to prevent the accidental spread of mutable state.

So, if you're willing to commit to a completely immutable domain model, you don't have object graphs as such anymore, you have object trees instead.

For a functionally-oriented approach to serialization, SBinary is what I'd probably try first. For cloning, Just Don't Do It. :)

like image 97
Alex Cruise Avatar answered Sep 19 '22 20:09

Alex Cruise


Or are marker interfaces still the right choice in Scala?

Nope. They aren't even the right choice in Java. They should be annotations, not interfaces.

like image 21
Jörg W Mittag Avatar answered Sep 19 '22 20:09

Jörg W Mittag