Error: constant false is not an lvalue
Jarrett Billingsley
jarrett.billingsley at gmail.com
Sun Aug 30 07:27:51 PDT 2009
On Sun, Aug 30, 2009 at 5:34 AM, Rainer Deyke<rainerd at eldwood.com> wrote:
> Jarrett Billingsley wrote:
>> Although teeeeeeechnically speaking that would be illegal code. The D
>> spec says that it's not legal to use the value of uninitialized
>> variables. Default initialization is kind of a poor man's substitute
>> for actual flow control which determines that. By relying on default
>> initialization, you're relying on what is actually nonconformant
>> behavior, and it could be broken in the future or by a smarter
>> compiler.
>
> No. This is an uninitialized variable in D:
>
> int i = void;
>
> This is an initialized variable in D:
>
> int i;
>
> A default-initialized variable is not in any way less initialized than
> any other initialized variable.
"It is an error to use a local variable without first assigning it a
value. The implementation may not always be able to detect these
cases. Other language compilers sometimes issue a warning for this,
but since it is always a bug, it should be an error."
http://www.digitalmars.com/d/1.0/function.html
I wouldn't be surprised if W himself has forgotten about this rule,
since other parts of the spec make no mention of it, or seem to depend
on the default-initialization of variables.
"If the Initializer is void, however, the variable is not initialized.
If its value is used before it is set, undefined program behavior will
result."
http://www.digitalmars.com/d/1.0/declaration.html
But wait.. the previous statement said that it was an error to use a
local without first assigning it a value. Meaning the code snippet
given there (int x = void; writefln(x);) is invalid.
> It cannot ever alter the behavior of the following perfectly legal D
> function:
>
> int get_zero() {
> int i;
> return i;
> }
I interpret the first statement as saying that this isn't, and never
has been, legal D, and that DMD simply has never checked for it. It's
also illegal to declare variables that are never used, but you don't
get an error for that either.
As so many things in the D spec, this is unclear. The current behavior
might actually be conformant to the first statement; it's just that
the default initialization has made it all but pointless. The
section(s) on default initialization of course do not mention how it
interacts with the first rule. (Similar to how bools can't have any
silly things like arithmetic operations performed on them (good!) but
can be implicitly cast to almost every other basic type, meaning you
can do it anyway (bad!).)
More information about the Digitalmars-d-learn
mailing list