Using the functions "map" and "any" on tuples in compile time.

realhet real_het at
Sun Apr 12 11:17:39 UTC 2020

Hello, anyone can help me make this better?

The functionality I want to achieve is: While serializing the 
fields of a struct, I want it to check the @STORED UDA on every 
field. If there is no fields are marked with @STORED, that means 
every field must be serialized. Otherwise only the marked one.

I had problems using map and any, so I come up with this lame 
foreach version: It works, but I think it does it in runtime.

     bool anySTORED = false;
     static foreach(fieldName; FieldNameTuple!T)
       mixin("anySTORED |= hasUDA!(data.*, STORED);".replace("*", 

     static foreach(fieldName; FieldNameTuple!T){{
       mixin("const thisSTORED = hasUDA!(data.*, 
STORED);".replace("*", fieldName));
       if(thisSTORED || !anySTORED) 
mixin("streamAppend_json!(dense, fieldName)(st, data.*, 
nextIndent);".replace("*", fieldName));

Thanks in advance!

(ps: I just love string mixins, I know :D)

More information about the Digitalmars-d-learn mailing list