labeled block stement.

monarch_dodra monarchdodra at gmail.com
Thu Dec 20 04:00:06 PST 2012


I had (some time ago), created a request for breakable labeled 
blocks:
http://d.puremagic.com/issues/show_bug.cgi?id=8622

Now apparently, there is a "LabeledStatement" in D:
http://dlang.org/statement.html#LabeledStatement

"Any statement can be labeled, including empty statements, and so 
can serve as the target of a goto statement. Labeled statements 
can also serve as the target of a break or continue statement."

Never mind that you *can't* break out of a LabeledStatement 
(*only* labeled if/for/while), what I find spectacularly 
confusing is that this:
LabeledStatement:
   Identifier : NoScopeStatement

"NoScopeStatement" means (apparently):

//---- Legal Code
int main()
{
   label:
   {
     int a = 4;
   }
   a = 4;
}
//----
//---- Illegal Code
int main()
{
   label1:
   {
     int a = 4;
   }
   label2:
   {
     int a = 4;
   }
}
//----
This behavior is because (apparently) a label followed by a "{}" 
does not create a new scope (!). Is this really what 
"NoScopeStatement" even means? I'd figure what I just wrote is a 
BlockStatement that's nested inside a NoScopeStatement...

I'm trying to wrap my head around: Is this valid and behaving 
according to spec?

In particular, if this is "according to spec", what is the 
rationale for having the spec that way anyways? How is this any 
better then what we have in C? I don't suppose we deviate just 
for the fun of deviating...?

In particular, I know I write BlockStatements all over the place, 
*especially* when labels and gotos start getting involved (to 
avoid jumping past declarations) I think this "feature" is at 
best useless, and at worst dangerous.

Anybody have any insight on why we even have this?


More information about the Digitalmars-d mailing list