initializing struct containing user defined type
Salih Dincer
salihdb at hotmail.com
Fri Feb 18 20:16:51 UTC 2022
On Friday, 18 February 2022 at 16:45:24 UTC, Ali Çehreli wrote:
> On 2/18/22 07:01, kdevel wrote:
>
> > Error: struct `B` has constructors, cannot use `{
> initializers }`,
> > use `B( initializers )` instead
> >
> > What is the rationale behind that? I mean: If the compiler
> exactly
> > sees what the program author intends to express why does it
> force the
> > author to change the code?
>
> I don't know the answer to that. The {} initializers always
> seemed out of place to me. I assumed they had to be supported
> to copy+paste C code to D and it should mostly work.
>
> One benefit of the {} initializer is being able use named
> initializers:
>
> struct S {
> int a;
> int b;
> }
>
> void main() {
> S s = { b : 2, a : 1 };
> }
>
> I still think it's out of place. :)
>
> I think that syntax will be obviated when D will have named
> arguments.
>
> Ali
```d
struct S {
float a, b;
@disable this(this);
}
enum par : float { a = 1, b = 2 }
S x = { b : par.b, a : par.a };
S y = S(par.a, par.b);
auto s1 = x.a / x.b;
auto s2 = y.a / y.b;
s1.writeln();
s2.writeln();
writeln(s1 + s2);
// x.writeln(y); // 2.087
"D Compiler v".writeln(__VERSION__/1000.0);
```
I'm using v2.087 but because of @disable this line doesn't work:
```// x.writeln(y); // 2.087```
Why? Can you give an explanation for this?
More information about the Digitalmars-d-learn
mailing list