Bug 20543 – Need a way to get the default initializers in an aggregation
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-01-29T09:14:01Z
Last change time
2022-07-04T17:44:00Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2020-01-29T09:14:01Z
Currently there are no traits to extract default initializers out of an aggregate type.
For example:
-----
struct S
{
int x = 1;
int y = 0;
int z;
}
-----
There is a workaround: Use S.init to retrieve default values.
However, it is not reliable. Because S.init == S(1, 0, 0), but in fact 'z' does not have a *user-provided* initializer, it only has an initializer due to T.init.
This distinction is important for things like Configuration structures.
I propose a simple API: __traits(hasInitializer, ...). Then, it can be composed with fairly simply:
static if (__traits(hasInitializer, S.x))
doSomething(S.init.x);
static if (!__traits(hasInitializer, S.z))
doSomethingElse();
Comment #1 by andrej.mitrovich — 2022-07-04T17:44:00Z
This is overkill and can easily be worked around with UDAs.