[Issue 8622] New: Allow labeled breaks to work on *any* block
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Sep 5 06:35:30 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8622
Summary: Allow labeled breaks to work on *any* block
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: monarchdodra at gmail.com
--- Comment #0 from monarchdodra at gmail.com 2012-09-05 06:35:59 PDT ---
D gives the possibility of multiple-breaking out (nested) while/for blocks
using labeled breaks.
I think it would be beneficial to extend this to being able to break out of any
"block", as long as it is labeled:
----
void main()
{
label: {
// ..code..
writefln("code1");
writefln("break..");
break label; // jumps to "here:"
// ..code..
writefln("code2");
}
// break lands here
writefln("here");
}
----
The main use case here would actually be to replace the "triangle if" pattern:
----
if(condition1)
{
if(condition2)
{
if(condition3)
{
if(condition4)
{
//DO SOMETHING
}
}
}
}
----
Which would become
----
conditional_block:
{
if(!condition1) break conditional_block;
if(!condition2) break conditional_block;
if(!condition3) break conditional_block;
if(!condition4) break conditional_block;
//DO SOMETHING
}
----
The usual "workaround" to the "triangle if" pattern is to move it to it's own
function, and *return* from it. The disadvantage is that it burdens the
developer with a whole extra function. The "breakable block" has the advantage
of not breaking the logic flow of the current function.
Another workaround, the "do_while_false" is also possible:
----
do:
{
if(!condition1) break;
if(!condition2) break;
if(!condition3) break;
if(!condition4) break;
//DO SOMETHING
}while(false);
----
However, my experience is that this pattern tends to be confusing for readers
that expected a loop: The actual condition "while(false)" is only documented
near the end.
Further more, given both the above workarounds, neither scale should you want
to nest a double break.
Conclusion: Generic breakable labeled blocks. It works better and is
clearer/more verbose.
Thank you/
--
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