Deriving a struct from another one via template: Easy way to propagate UDAs?

Inkrementator anon at example.org
Thu Mar 14 23:19:37 UTC 2024


Hello,
I am trying to derive a struct from another. I want to modify 
each field such that type of it goes from some T to Nullable!T, 
preserving all fieldnames and UDAs.

I think that fieldnames and UDAs can only be duplicated via 
string-mixins. This means that all field-types that aren't 
visible in the template scope have to first be aliased to some 
name that can be referenced in the string mixin. I got this to 
work for simple types, but as soon as the field types are some 
templated type, doing this naively stops to work.
Same story for UDAs. As soon as multiple unknown types are 
involved, I can't simply take an alias anymore.

The one workaround I found is to make my introspective struct 
part of a template mixin, this way it can access all symbols of 
the callsite and can use all the symbols. Is this the recommended 
way to do it? Generating a string mixin directly would probably 
work just as well, but that seems even more ugly and will 
probably be more susceptible to collisions.

This not so simple gist demonstrates what I tried so far. 
injectedNameFields generates a string that when mixed in, will 
create all the field names with the desired types. I suggest 
first looking at main and compiling+running to get an idea what 
is supposed to happen.
https://gitlab.com/-/snippets/3687470

After describing my problem, here is a non-exhaustive list of 
questions I have:
* Is UDA propagation possible without string mixins?
* To make it run without template mixin, I will need to write a 
recursive template (recursive over template args as well as UDA 
instantiated structs) that accounts for cases: normal type, 
templated type, enum (C style), enum value compile time constant, 
etc. ... Is this correct, or is there a way to just grab any 
compile time alias/enum without giving each "type" special 
treatment?
* Are template mixins vulnerable to name collisions?


More information about the Digitalmars-d-learn mailing list