Avoid initializing a struct field.

Marco Leise Marco.Leise at gmx.de
Sun May 5 13:07:54 PDT 2013


Am Sat, 04 May 2013 21:00:17 +0200
schrieb "jerro" <a at a.com>:

> I tried using float[42] instead of int[42] and found out that 
> buffer actually isn't initialized to its default initializer if I 
> use "= void" (the numbers were all 0 instead of NaN), but the 
> performance cost is still there.

Zero is the magic number. A struct with only binary zeros goes
to the BS section in the executable (on Windows*), taking up no
file space. So it serves a purpose as is. The .init property
is a key part of the language. It is used in equality
comparisons as well as a template that you can create carbon
copies of. "T.init" might be the default value for a parameter
of templated function, or it could be used as the initial
value for new entries in a list/table with code relying on it
being a constant.
Your point is valid to some extent, but while logical it adds
complexity and increases the odds that someone doesn't
expect .init to be a _random_ value. Because that's what it is
to templated code that is instantiated with such a struct.

*) http://d.puremagic.com/issues/show_bug.cgi?id=7319

-- 
Marco



More information about the Digitalmars-d-learn mailing list