struct initializer

Dom DiSc dominikus at scherkl.de
Thu Nov 30 07:21:29 UTC 2023


On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote:
> You can use this syntax without an explicit constructor:
>
>     struct S3 { int a; int b; }
>
>     S3 fun() { return S3(5, 2); }
>
> The language spec calls this a struct literal

Ok, so we have

```d
struct S { int a; int b; }

S s = S(5, 3); // works
s = S(6, 2); // works
S fun() { return S(5, 2); } // works
int fun2(S s2);
fun2(S(4,4)); // works
```

but

```d
struct S { int a; int b; }

S s = { 5, 3 }; // works
s = { 6, 2 }; // doesn't work
S fun() { return { 5, 2 }; } // doesn't work
int fun2(S s2);
fun2(S(4,4)); // doesn't work
```

So, why supporting the (somewhat strange looking) version with 
curly backets at all?
It only works in one special place, so is simply overhead to 
remember.
Again a superfluous way to do the same - but only under specific 
circumstances.

I think a syntax should work either always or never.


More information about the Digitalmars-d-learn mailing list