On Tue, May 03, 2022 at 05:25:06PM +0000, cc via Digitalmars-d-learn wrote: > On Tuesday, 3 May 2022 at 17:05:09 UTC, H. S. Teoh wrote: > > Oops, sorry, I made a mistake. The definition of Serializable should be: > > > > class Serializable(Base, Derived = Object) : Base {} > > There we go, works with this, now I get what it's trying to do: > ```d > class Serializable(Base, Derived = Object) : Derived { > ``` > > What's the purpose of the `static struct Proxy`? The `static this()` > seems to work without being enclosed in a structure.
Actually, come to think of it, Proxy isn't strictly necessary. You could move the static this into Serializable and it'd work. The main purpose here is to create a separate instance of static this() per instantiation of Serializable, i.e., there'd be a separate instance of static this() for each Derived class. Since the compiler collects all static this()'s into a list of functions that run at program startup, this allows us to initialize the deserializers AA with functions that understand how to create an instance of Derived at runtime. Since in the scope of the static this we have direct access to compile-time information about Derived, it can use compile-time introspection to inspect Derived and take the appropriate action at runtime based on this compile-time knowledge. In this way, we "translate" compile-time knowledge into runtime knowledge. T -- PNP = Plug 'N' Pray