digitalmars.D - Suggestion : Extensions to 'scope' statement
- Steve Horne (48/48) Sep 06 2006 I have fallen in love with the scope statement ;-)
I have fallen in love with the scope statement ;-) One thing, though. Success vs. failure isn't always a matter of exception throws. There is a less fatal kind of failure that is often indicated by a return value. For instance, if I have a smart iterator with a 'Goto_Next' method, it will probably return false if it fails. After all, failure isn't all that exceptional - it happens at the end of every loop. This isn't a case of 'I should be using exceptions but I'm stupid', it's a case where exceptions are inappropriate. Readability and maintainability would get worse if I used them. Therefore, how about a "scope(return argname)". For example... bool Example () { Save_State (); scope(return p) { if (p) { Discard_Saved_State (); } else { Restore_Saved_State (); } } // Do stuff } In a large function, this could save a lot of hassle. Except for one thing. What if the scope exits some other way. In the above example, I could just use 'scope(failure)', but what if the scope statement was in an inner block that could be exited by a goto or running off the end or whatever? Do we want a huge selection of 'scope' types so that every possible exit route can be specified unambiguously? Well, no, not really. So, how about... bool Example () { Save_State (); scope(return p) { if (p) { Discard_Saved_State (); } else { Restore_Saved_State (); } } else scope(failure) { Restore_Saved_State (); } else { // Can't happen here, but I'm sure you get the idea. } // Do stuff }
Sep 06 2006