www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20543] New: Need a way to get the default initializers in an

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 puremagic.com
          Reporter: andrej.mitrovich 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();

--
Jan 29 2020