initializing struct containing user defined type

kdevel kdevel at vogtner.de
Fri Feb 18 15:01:58 UTC 2022


On Friday, 18 February 2022 at 14:37:25 UTC, Ali Çehreli wrote:
> On 2/18/22 06:19, kdevel wrote:
>
> >     //   auto b3 = B ("A", "B"); // Error: cannot implicitly
> convert
> >     // expression `"A"` of type `string` to `A`
>
> Yeah, D disallows some implicit conversions.
>
> Adding a constructor to B will make it work:
>
>   this(string as, string bs) {
>     this.a = A(as);
>     this.b = A(bs);
>   }
> }

Okay. b0 needs an additional constructor with signature this (A, 
A).
The initialization of b3 is now no longer working. dmd says:

    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?



More information about the Digitalmars-d-learn mailing list