goto a no-go?
monarch_dodra
monarchdodra at gmail.com
Tue Oct 1 06:44:28 PDT 2013
On Tuesday, 1 October 2013 at 13:09:01 UTC, Dicebot wrote:
> On Tuesday, 1 October 2013 at 13:02:56 UTC, monarch_dodra wrote:
>> On Tuesday, 1 October 2013 at 12:46:50 UTC, Dicebot wrote:
>>> Can't you use scope guards for that too? Those work for any
>>> scope, not only function scope.
>>
>> I hadn't thought of that before, but I don't think so. scope
>> guards are designed to run a specific piece of code, no mater
>> the code path.
>>
>> What I mostly want to do is just break out of a "control
>> block".
>>
>> Either that, or I'm being retarded...?
> (I think your snippet misses actual labels so it was unclear:))
Right, sorry.
> In that case you can always replace "control block + goto" with
> nested function + return + scope guard inside it (if some
> cleanup code needs to be run) unless I am missing something.
I was going to say: "but using functions is a pain when you have
variables that are in scope you need to reuse", but I keep
forgetting that D can indeed nest functions.
The only "limitation" of using a nested function, as opposed to a
"breakable block", is that can't move code into a nested
function, if it contains a (conditional) return.
I guess the conclusion is that each code snippet is unique. With
enough effort, you can probably eliminate goto from almost any
example. I do find that sometimes, you tend to have to jump
through hoops for that tough, and the final code is not
necessarily "better" either.
More information about the Digitalmars-d
mailing list