Struct assignment fails, why?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jun 16 20:54:07 UTC 2021


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

-- 
Everybody talks about it, but nobody does anything about it!  -- Mark Twain


More information about the Digitalmars-d-learn mailing list