Getting the initial value of a class field in compile time.

realhet real_het at hotmail.com
Sun Feb 9 01:10:56 UTC 2020


On Sunday, 9 February 2020 at 00:57:05 UTC, realhet wrote:
> On Sunday, 9 February 2020 at 00:41:12 UTC, realhet wrote:
>> On Sunday, 9 February 2020 at 00:27:21 UTC, Adam D. Ruppe 
>> wrote:


     mixin([FieldNameTuple!T].map!(n => n~"=(new T)."~n~";").join);

After checking it in the asm debugger, it turned out, that it 
evaluates (new T) in runtime. (Silly me, because the constructor 
can have side effects.)
So I first put the default values into an enum, and then do the 
assignment:

     foreach(n; FieldNameTuple!T){{
       mixin("enum def = (new T).@; @ = def;".replace("@", n));
     }}

And that became as fast as it can be:

     mov dword ptr ds:[rax+10], 2A

Thanks for the help again.


More information about the Digitalmars-d-learn mailing list