Check if a variable exists

Kirk McDonald kirklin.mcdonald at gmail.com
Fri Aug 18 16:07:46 PDT 2006


Peter Thomassen wrote:
> Kirk McDonald schrieb am Freitag, 18. August 2006 21:39:
> 
>>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?

[test.d]
import std.stdio;

void main() {
     for (;;) {
         int j = 10;
         writefln("j = ", j);
         break;
     }
     writefln("j = ", j); // Line 9
}
// EOF

$ dmd test
test.d(9): undefined identifier j

Seems to be true to me.

> 
>>Example 2: Declare inside the loop
>>
>>for (;;) {
>>     int temp;
>>     // stuff
>>}
>>// temp doesn't exist anymore; its scope ended
> 
> 
> See above.
> 
> 
>>Static if is analogous to #if/#endif in the C preprocessor. It does not
>>apply here, so nevermind what I told you. :-)
> 
> 
> Mh ... I only know PHP :-)
> Peter


-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d-learn mailing list