Suppose I have a class C that is defined as a mixin from traits T1, T2, T3. Each trait has a method named archive that writes the variables declared in that trait to an output stream. When I archive an instance of class C, I want it to archive each of its traits. How may I write this without ambiguity? I don’t want to make a unique name for the archiver in each trait (e.g. def archiveT1, def archiveT2 etc). Is it possible for an instance of C to do something like T1.archive(), T2.archive() etc. even though archive would not be static?
Look up ‘Scala trait linearisation’ and all will be revealed. The idea is to only make calls of the form super.archive
that magically propagate up the linearised trait chain.
That said, have you considered using Kryo, rather than rolling your own solution?
There are several other libraries such as Circe, micro-Pickle that will take case class instances to and from JSON, but if you want something like Java serialisation, only a lot more performant, I find Kryo is a good choice.
If you go down the Kryo road, look up the project for the Akka/Pekko Kryo serialisers and don’t bother with Chill. Long story, trust in me, as Shere Khan would say.
Example project: curium/src/main/scala/com/sageserpent/curium/SerializationFacade.scala at master · sageserpent-open/curium · GitHub
@sageserpent-open: Thanks. So that’s what trait linearisation is for. I had hoped it would be as simple as that. I have used other serialising methods in the past, including the ones you mention, but I prefer my own solution, which is very fast, and it also archives cyclic object graphs into a choice of either JSON or a Lisp-like syntax that is faster and easier to parse than JSON. The archiving model is based on NextStep’s archiving model.