```D
struct Test
{
int test;
}
struct Data
{
Test[8] test;
}
void add(Data data)
{
}
extern(C) void main()
{
add( Data(test: [ {test: 1}] ) );
}
```
This doesn't compile, it should
Comment #1 by razvan.nitu1305 — 2024-09-12T12:49:57Z
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.
Comment #2 by razvan.nitu1305 — 2024-09-12T13:06:52Z
(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)] ) );
}
Comment #3 by nick — 2024-09-13T09:35:38Z
> `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 `(){...}`.
Comment #4 by robert.schadek — 2024-12-13T19:37:21Z