effect of a label on following block

Nick Voronin elfy.nv at gmail.com
Mon Nov 15 07:07:29 PST 2010


Hello.

Consider this code

void main()
{
l:
   {
     int v;
   }
   v = 5; // ok, v is defined
}

As I understand from D's grammar this behaviour is not a bug as

LabeledStatement:
     Identifier : NoScopeStatement

and NoScopeStatement in turn takes BlockStatement without creating new  
scope.

It looks very unnatural for me though. Especially when label goes before  
ThenStatement

if(1)
l1:{
   int v;
}
v = 5;

it works as above, yet label after 'while'

while(1)
l1:{
   int v;
}
v = 5; // error, v is undefined

has no such effect, even if ThenStatement is just another ScopeStatement.  
I don't understand this difference. Any rationale behind this?

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list