Struct assignment fails, why?

Brian bcallah at openbsd.org
Wed Jun 16 22:37:47 UTC 2021


On Wednesday, 16 June 2021 at 20:54:07 UTC, H. S. Teoh wrote:
> On Wed, Jun 16, 2021 at 08:44:46PM +0000, Brian via 
> Digitalmars-d-learn wrote: [...]
>> struct item
>> {
>>     string name;
>>     int type;
>> };
> [...]
>>             new_item = { "item1", 1 };
>
> The {...} initializer syntax is only available in variable 
> declarations, e.g.:
>
> 	item i = { "item1", 1 };
>
> You cannot use this syntax in assignment statements.
>
> A simple alternative is to use constructor syntax for 
> constructing an instance of the struct:
>
> 	new_item = item("item", 1);
>
>
> T

Gotcha. Thanks.

~Brian


More information about the Digitalmars-d-learn mailing list