What is the stance on partial initializers when declaring multiplevariables of the same type?
Nick Sabalausky
a at a.a
Thu Jul 21 14:22:54 PDT 2011
"Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote in message
news:mailman.1826.1311231133.14074.digitalmars-d at puremagic.com...
>I ran into this simple C declaration:
>
> float float_x, float_y, float_xb, float_yb;
>
> These need to be explicitly initialized in D, otherwise you either get
> crashes or you won't get anything but a blank screen (with regards to
> rendering with OpenGL).
>
> Almost instinctively I went for:
>
> float float_x, float_y, float_xb, float_yb = 0.0;
>
> But that's incorrect, only float_yb is zero-initialized, the others
> are initialized to NaN (well I predicted that but I was kinda hoping D
> would be cool and use a common initializer).
>
> What I'm asking is, are partial initializers useful for people? I'm
> not suggesting any changes, but just starting a discussion. It does
> look like this could maybe introduce bugs. I think someone might have
> mentioned this topic before, but I don't recall.
>
> Here's to a healthy discussion..
It's an interesting point.
I find that I rarely declare more than one variable in a single statement.
But if I were to do so, then most likely they would be such similar
variables that I likely would want them inited to the same value. Can't
think of a good syntax for it though. "float a, b, c = 0.0;" definitely
makes it look like you're only trying init 'c'.
But this is an interesting inconsistency between assignments and
initializations. I mean, it's trivial to *assign* multiple variables to the
same value:
a = b = c = 0.0;
*Normally* the only syntactical difference between assignment and
initialization is whether or not "x=y;" is preceeded by a type. But with
multiple assignments as above, there's no initialization equivalent to that.
Crazy, nutty, wacky idea...
float (a, b, c) = 0.0;
...?
More information about the Digitalmars-d
mailing list