Check if a variable exists

Unknown W. Brackets unknown at simplemachines.org
Tue Aug 22 07:17:11 PDT 2006


Many C compilers often kept values going from fors, e.g.:

for (int i = 0; i < 5; i++)
	foo(i);

// i still exists.

But D does not.  Anyway, it would still always exist or not exist, this 
is just changing when.

It might help to understand that PHP uses a hash table for variables. 
It's kinda like doing this:

int[char[]] vars;

if (cond)
	vars["i"] = 1;

if ("i" in vars)
	writefln("i is set to %d.", vars["i"]);
else
	writefln("is is not set.");

But... that's not going to be as efficient as using a standard variable 
(int i.)

-[Unknown]


> Kirk McDonald schrieb am Samstag, 19. August 2006 01:07:
>>>> Oh. Then you don't want this at all. Use the bool. :-) The reason is
>>>> simple: if you declare the temporary before the for loop, then it will
>>>> always exist after the for loop. If you declare the temporary inside the
>>>> for loop, then it only has the scope of the for loop; it will cease to
>>>> exist when the loop ends. Whichever way you do it, whether it exists or
>>>> not after the for loop never varies.
>>> This is not true, I tested it (using a while, not a for).
>> Hmm?
>>
>> [...]
>>
>> $ dmd test
>> test.d(9): undefined identifier j
>>
>> Seems to be true to me.
> 
> Yeah ... I think I can remember a case where this was not true, but probably
> I haven't noticed a declaration some lines before the loop.
> 
> Sorry,
> Peter



More information about the Digitalmars-d-learn mailing list