CRTP + compile-time introspection + static ctors = WIN

Jacob Carlborg doob at me.com
Sat Jan 23 09:27:08 UTC 2021


On 2021-01-18 18:15, H. S. Teoh wrote:

> I also considered this approach, but rejected it because forgetting to
> register a derived class would result in incorrect serialization. I felt
> that was too fragile for my needs.

Orange will catch this at runtime and throw an exception.

> My current serialization need is quite specific in scope: I have a bunch
> of arrays, AA's, structs, and classes, all of which are data-only (i.e.,
> public data fields only, no special semantics via setters/getters).  A
> small number of types may require special serialization/deserialization
> treatment; for this the serialization code detects the existence of
> custom .save/.load methods.  Other than that, serialization is automated
> from the root object.  Since root objects are very general, and as
> development goes on the exact combination of contained types may change,
> so a solution that does not require explicit registration of types is
> ideal.
> 
> Using my solution above, the only thing I need to check is that the
> derived class derives from Saveable, which can be done the first time I
> declare it. It's highly visible, so accidental omission can be
> immediately noticed.  Nothing else needs to be done, as the rest of the
> mechanisms are fully automated from that point on.

I think both of our solutions are equally automatic. You require 
inheriting from a specific class, mine require registering the class.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list