D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20543 - Need a way to get the default initializers in an aggregation
Summary: Need a way to get the default initializers in an aggregation
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-29 09:14 UTC by Andrej Mitrovic
Modified: 2022-07-04 17:44 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2020-01-29 09:14:01 UTC
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 Andrej Mitrovic 2022-07-04 17:44:00 UTC
This is overkill and can easily be worked around with UDAs.