[Issue 8622] Allow labeled breaks to work on *any* block

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 19 07:38:22 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8622



--- Comment #3 from Dmitry S <ds.dlang at gmail.com> 2012-12-19 07:38:20 PST ---
This feature is related to an "expectation bug", in that current spec is very
counterintuitive regarding labeled blocks. I added a couple of "See Also" bugs
where the current behavior of labeled blocks has tripped people up. The
problems are best demonstrated by a code sample (taken from those issues):


    // Problem 1 (not a bug according to spec, but entirely counterintuitive).
    int n;
  label:
    {
        scope(exit) n++;
        n=3;
    }
    writefln("n=%d should be 4", n); // Prints "n=3 should be 4"



    // Problem 2: (counterintuitive AND a bug because should not compile).
  block:
    {
        for (int i = 0; i < 10; i++) {
            if (i == 5) break block;
        }
        writefln("Within block");   // This line IS printed.
    }
    writefln("Outside block");


The common problem for both cases is that a labeled block adds curly braces,
but the code behaves as if the braces weren't there.

In particular, adding a label to a block makes the block's scope disappear.
Consider how the first example would change if "label:" line is commented out.

That trips people up. It could be solved perhaps by simply prohibiting labels
on a block, but monarchdodra's suggestion seems like an really nice solution,
allowing for very intuitive flexibility (e.g. the second example is equivalent
to python's "else" clause on loops, more intuitive, and nearly as concise).

(Note that monarchdodra's suggestion critically depends on labeled blocks.
Unlabeled breaks definitely need to break out of the innermost loop or switch.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list