DIP 1031--Deprecate Brace-Style Struct Initializers--Community Review Round 1 Discussion

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Feb 21 15:19:34 UTC 2020


On Saturday, February 15, 2020 5:50:15 AM MST Guillaume Piolat via 
Digitalmars-d wrote:
> On Thursday, 13 February 2020 at 08:52:41 UTC, Paolo Invernizzi
>
> wrote:
> > Anyway, the DIP is fine, I've always hated brace-style struct
> > initializers, let's kill them with fire.
>
> 13 years of D and I didn't know brace-style struct initializers
> even existed... Sounds error prone.

I tend to forget that the syntax exists, and I never use it, but it's
probably less error-prone than using the constructor syntax with a struct
that has no constructors, since it's at least lining up the field
initializers with the fields by name, meaning that if fields are added,
removed, and/or rearranged over time, you won't end up with existing code
initializing the wrong fields.

If you use the constructor syntax with a struct that has no constructors,
then it just sets the fields in order, and if you provide fewer initializers
than there are fields, then the rest just get default-initialized. And while
that can be fine when you first declare the struct, it can be very
error-prone if the struct's fields are ever changed later, since existing
code may still compile but then end up setting the wrong fields. It's why I
pretty much always declare constructors for structs even if they're just POD
types where all of the members are public.

- Jonathan M Davis





More information about the Digitalmars-d mailing list