[Issue 20543] New: Need a way to get the default initializers in an aggregation

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 29 09:14:01 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=20543

          Issue ID: 20543
           Summary: Need a way to get the default initializers in an
                    aggregation
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: andrej.mitrovich at gmail.com

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();

--


More information about the Digitalmars-d-bugs mailing list