Thread overview
[Issue 24763] Can't initialize a struct with a static array
Sep 12
RazvanN
Sep 12
RazvanN
[Issue 24763] Can't use struct initializer in an array literal
September 12
https://issues.dlang.org/show_bug.cgi?id=24763

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
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.

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

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

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

--- Comment #2 from RazvanN <razvan.nitu1305@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)] ) );
}

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

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick@geany.org
            Summary|Can't initialize a struct   |Can't use struct
                   |with a static array         |initializer in an array
                   |                            |literal

--- Comment #3 from Nick Treleaven <nick@geany.org> ---
> `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

Well then the parser would have to look ahead in function literals for a semi-colon after an arbitrarily complex expression statement. Unless we make `{...}` not a function literal and require `(){...}`.

--