www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - labeled block stement.

reply "monarch_dodra" <monarchdodra gmail.com> writes:
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?
Dec 20 2012
parent Nick Treleaven <ntrel-public yahoo.co.uk> writes:
On 20/12/2012 12:00, monarch_dodra wrote:
 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?
I think so, judging from Walter's comments in the bug reports.
 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?
I for one can't work out why anyone working to the spec would want to put braces after a label, knowing that they have no effect. It's very unintuitive. I think it is a bug-prone design, considering the bug report examples.
Dec 21 2012