On Thursday, 21 April 2016 at 11:53:13 UTC, rumbu wrote:
On Wednesday, 20 April 2016 at 12:32:48 UTC, Tofu Ninja wrote:
Is there a way to shallow copy an object when the type is
known? I cant seem to figure out if there is a standard way. I
can't just implement a copy function for the class, I need a
generic solution.
extern (C) Object _d_newclass(TypeInfo_Class ci);
Object dup(Object obj)
{
if (obj is null)
return null;
ClassInfo ci = obj.classinfo;
size_t start = Object.classinfo.init.length;
size_t end = ci.init.length;
Object clone = _d_newclass(ci);
(cast(void*)clone)[start .. end] = (cast(void*)obj)[start
.. end];
return clone;
}
Is this the established D-idiom of a generic class bit-wise clone
function (as of 2020)?
Any risk of using this implementation I should be aware of?
Thanks.