www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check if a variable exists

reply Peter Thomassen <info peter-thomassen.de> writes:
Hi.

Is it possible to check if a variable exists, like PHP's isset()?

Thanks!
Peter
Aug 18 2006
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Peter Thomassen wrote:
 Hi.
 
 Is it possible to check if a variable exists, like PHP's isset()?
 
 Thanks!
 Peter
Typically, you only ever have to ask when doing fairly complex template stuff; this is largely the point of a statically-typed language. Mind if I ask why you need to check for this? The answer, though, is this: static if (is(typeof(my_variable))) { // ... } -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Aug 18 2006
parent reply Peter Thomassen <info peter-thomassen.de> writes:
Kirk McDonald schrieb am Freitag, 18. August 2006 21:14:
 Is it possible to check if a variable exists, like PHP's isset()?
Typically, you only ever have to ask when doing fairly complex template stuff; this is largely the point of a statically-typed language. Mind if I ask why you need to check for this?
I've got a for loop which contains an if statement checks something depending on the counting loop var i. If true, some action producing a temporary variable is taken. After the for loop, I want to do further things if and only if the "if" tree has been entered at least once. I cannot check that using i because that has modified during the loop. Of course I could declare a bool in the "if" tree and check for that, but I know that I will have the temporary variable set, if the "if" tree has been entered. So, why not check for this?
 The answer, though, is this: 
 
 static if (is(typeof(my_variable))) {
      // ...
 }
Thanks. What is the static keyword for? Peter
Aug 18 2006
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Peter Thomassen wrote:
 Kirk McDonald schrieb am Freitag, 18. August 2006 21:14:
 
Is it possible to check if a variable exists, like PHP's isset()?
Typically, you only ever have to ask when doing fairly complex template stuff; this is largely the point of a statically-typed language. Mind if I ask why you need to check for this?
I've got a for loop which contains an if statement checks something depending on the counting loop var i. If true, some action producing a temporary variable is taken. After the for loop, I want to do further things if and only if the "if" tree has been entered at least once. I cannot check that using i because that has modified during the loop. Of course I could declare a bool in the "if" tree and check for that, but I know that I will have the temporary variable set, if the "if" tree has been entered. So, why not check for this?
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. Example 1: Declare before the loop int temp; for (;;) { // stuff } // temp exists here Example 2: Declare inside the loop for (;;) { int temp; // stuff } // temp doesn't exist anymore; its scope ended
 
The answer, though, is this: 

static if (is(typeof(my_variable))) {
     // ...
}
Thanks. What is the static keyword for? Peter
http://www.digitalmars.com/d/version.html#staticif Static if is analogous to #if/#endif in the C preprocessor. It does not apply here, so nevermind what I told you. :-) -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Aug 18 2006
parent reply Peter Thomassen <info peter-thomassen.de> writes:
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).
 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
Aug 18 2006
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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
Aug 18 2006
parent reply Peter Thomassen <info peter-thomassen.de> writes:
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
Aug 21 2006
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
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
Aug 22 2006
prev sibling parent nobody <nobody mailinator.com> writes:
Peter Thomassen wrote:
 Kirk McDonald schrieb am Freitag, 18. August 2006 21:14:
 Is it possible to check if a variable exists, like PHP's isset()?
Typically, you only ever have to ask when doing fairly complex template stuff; this is largely the point of a statically-typed language. Mind if I ask why you need to check for this?
I've got a for loop which contains an if statement checks something depending on the counting loop var i. If true, some action producing a temporary variable is taken. After the for loop, I want to do further things if and only if the "if" tree has been entered at least once. I cannot check that using i because that has modified during the loop. Of course I could declare a bool in the "if" tree and check for that, but I know that I will have the temporary variable set, if the "if" tree has been entered. So, why not check for this?
It is strictly not possible to introduce a variable into one scope from a scope which it encloses. This means any vars you define inside a function will not be available outside. Also loops have their own scope and so any variable you declare inside the loop is gone once the loop completes. The way to do what you want is by declaring a bool var /before/ the loop and then using that var inside the loop. Here is an example that demonstrates how to do what you want: bool isPrime(ubyte n) { bool prime = true; for(uint i = 2; i < ubyte.max; i+=1) { if( n != i ) { if( (n%i) == 0 ) prime = false; } } // i now out of scope and undefined return prime; } int main(char[][] args) { for(ubyte n = 2; n < ubyte.max; n+=1) { if( isPrime(n) ) dout.writeLine(toString(n)~" is prime."); else dout.writeLine(toString(n)~" is not prime."); } return 0; }
 
 The answer, though, is this: 

 static if (is(typeof(my_variable))) {
      // ...
 }
Thanks. What is the static keyword for? Peter
The reason people slipped away without answering this question is that static is very much like smurf. It can mean so many things: class Example { static int func() { static if(a) static int b; else static if(z) { static float y; int b = cast(int) y; } return ++b; } } The conditional compilation section of the D lang spec explains 'static if.' Conditional Compilation http://digitalmars.com/d/version.html A StaticIfConditional condition differs from an IfStatement in the following ways: ... 4. It must be evaluatable at compile time. That means since you will only know when the program is running whether an if statement succeeded static if is of no use.
Aug 20 2006