D Best Practices: Default initializers for structs

Jonathan M Davis jmdavisProg at gmx.com
Sat Nov 6 21:28:09 PDT 2010


Here's an interesting thought. All built-in types in D default initialize to the 
closest thing that they have to an error. Floating points arguably do the best 
job of this with NAN, and integral types arguably the worst with 0, but that is, 
as I understand it, the basic idea: default initializations initialize to an 
error value (or as close to one as you can get). That way, it quickly becomes 
obvious when you failed to properly initialize one before using it. So, the 
question is this: what about structs?

Structs default initialize to whatever their member variables are directly 
initialized to. That may or may not be an error state, but I don't get the 
impression that people generally _try_ and make it an error state. What should 
be the best practice on this? Should we generally _try_ and make struct 
initializers initialize to error states, just like the primitive types do - with 
the idea that you really are supposed to initialize them or assign to them 
before you use them. Or should we treat structs differently and try and make 
their default states something other than an error state?

By no means am I claiming that we should _always_ try and make a struct's init 
property an error or _always_ make it valid (that's going to depend on what 
exactly the struct is for and what it does), but which would be the best 
practice in the general case?

- Jonathan M Davis


More information about the Digitalmars-d mailing list