www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - if statement.

reply "Simen Haugen" <simen norstat.no> writes:
From the documentation:

"If an auto Identifier is provided, it is declared and initialized to the 
value and type of the Expression. Its scope extends from when it is 
initialized to the end of the ThenStatement.

If a Declarator is provided, it is declared and initialized to the value of 
the Expression. Its scope extends from when it is initialized to the end of 
the ThenStatement. "

I can't understand what Declerator is.. I thought it would assign a 
previously defined variable, but that's not the case.

 if (auto r = true) {
  writefln(r); // OK
 }

 bool r;
 if (r = true) { // Error: '=' does not give a boolean result
  writefln(r);
 }


So... What is it then?
Oct 22 2007
next sibling parent Regan Heath <regan netmail.co.nz> writes:
Simen Haugen wrote:
 From the documentation:
 
 "If an auto Identifier is provided, it is declared and initialized to the 
 value and type of the Expression. Its scope extends from when it is 
 initialized to the end of the ThenStatement.
 
 If a Declarator is provided, it is declared and initialized to the value of 
 the Expression. Its scope extends from when it is initialized to the end of 
 the ThenStatement. "
 
 I can't understand what Declerator is.. I thought it would assign a 
 previously defined variable, but that's not the case.
 
  if (auto r = true) {
   writefln(r); // OK
  }
 
  bool r;
  if (r = true) { // Error: '=' does not give a boolean result
   writefln(r);
  }
 
 
 So... What is it then?
This is a guess, but it compiles so... if (bool r = true) { writefln(r); } A declarator would be where you declare a variable, not re-assign. I guess. Regan
Oct 22 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Simen Haugen" <simen norstat.no> wrote in message 
news:ffi0d1$vjp$1 digitalmars.com...
 From the documentation:

 "If an auto Identifier is provided, it is declared and initialized to the 
 value and type of the Expression. Its scope extends from when it is 
 initialized to the end of the ThenStatement.

 If a Declarator is provided, it is declared and initialized to the value 
 of the Expression. Its scope extends from when it is initialized to the 
 end of the ThenStatement. "

 I can't understand what Declerator is.. I thought it would assign a 
 previously defined variable, but that's not the case.

 if (auto r = true) {
  writefln(r); // OK
 }

 bool r;
 if (r = true) { // Error: '=' does not give a boolean result
  writefln(r);
 }


 So... What is it then?
The spec for Declarators is given in the Declarations section of the spec. Yes, it's basically a variable declaration. Hence the name.
Oct 22 2007