scope(exit) considered harmful

Justin Johansson no at spam.com
Sun Nov 8 02:46:05 PST 2009


Long ago in the days before D, I was used to commenting out code with one of
two idioms:

#if false
.. this code is not compiled
#endif

or

if (false) {
.. this code is compiled but not executed
}

and to turn stuff back on again:

#if true
.. this code is back on now
#endif

or

if (true) {
.. this code is back on now too
}

but in the D world beware the Ides of Scopes because if your curly
brace block with the true/false conditional contains a scope(exit)
you will get explainable but unexpected behaviour if this code
is enabled inside an if(true) block.

Gotcha.

If scope(exit) is meant to be some mechanism for saving try/finally
boiler-plate code, it is a can of worms, otherwise it is a can of
i-dont-know-what-it's-good-for.

To my way of thinking, and no doubt I should go back to remedial CS-101,
semantics of scope(exit)  should related to function scope and not
curly-brace scope-statement scope.

Whilst an experienced coder (C/C++) and well understand what's
happening here in D, I feel compelled to say that this would have
to be a nightmare come true for newcomers to D.

My advice, stick to try/catch/finally and avoid D scope(exit) et. al.

-- Justin Johansson




More information about the Digitalmars-d mailing list