[Issue 24763] Can't initialize a struct with a static array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Sep 12 13:06:52 UTC 2024


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

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|blocker                     |enhancement

--- Comment #2 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to RazvanN from comment #1)
> A reduction of the test case:
> 
> struct Test
> {
>     int test;
> }
>   
> void func(Test t) {}
>   
> void main()
> {
>   
>     Test a = {test: 1}; 
>     func({test: 1});                                                        
> 
> }
> 
> It seems that struct initializers only work when they are the rhs of an
> assign expression.

Now I get it. The struct initializer is an initializer, meaning that it's not
an expression. Initializer can only appear as part of variable declarations.
For example:

Test a;
a = {test: 1};

Doesn't compile either because `a = {test: 1};` is an assign expression and the
struct initalizer is not an expression. I think this all could be simplified
and indeed the struct initializer should be accepted as an expression, however,
this is not a bug, rather an enhancement request.

If you are blocked by this, just use the default constructor:

struct Test
{
    int test;
}

struct Data
{
    Test[1] test;     // note that I changed this from Test[8] to Test[1]
                      // otherwise I had to put an array of 8 elements          
}

void add(Data data)
{
}

void func(Test t) {}

extern(C) void main()
{

    Test a = {test: 1};

    add( Data(test: [ Test(1)] ) );
}

--


More information about the Digitalmars-d-bugs mailing list