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

后端 未结 3 1470
情歌与酒
情歌与酒 2021-02-08 08:55

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.Seriali

3条回答
  •  一向
    一向 (楼主)
    2021-02-08 09:17

    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. :)

提交回复
热议问题