The terrible way is something like: void reset(Object o) in { assert(!(o is null)); } body { auto p = cast(ubyte*)*cast(void**)&o; auto ci = o.classinfo; auto init = cast(ubyte)ci.init; p[0..init.length] = init[]; if (ci.defaultConstructor) { ci.defaultConstructor(o); } else { throw new Exception("no default constructor; object is in invalid state"); } }
An object reference is just a pointer, but we can't directly cast it. So we make a pointer to it and cast that; the type system allows it. Now we can access the data that the object reference refers to directly. `o.classinfo` gets the runtime type information object for the actual class that `o` belongs to. The `init` property gives you a single blob of bytes representing the default field values of that class. So we copy that over into the object. To finish up, if the type has a default constructor, we invoke it. We don't have a way to identify any other constructors that we could invoke. This is usually not a good thing to do, but if you really need to, you can.