Should scope(exit) be valid outside of a scope?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed May 12 08:57:38 PDT 2010


Simen kjaeraas wrote:
> Graham Fawcett <fawcett at uwindsor.ca> wrote:
> 
>> On Tue, 11 May 2010 22:18:29 +0200, Don wrote:
>>
>>> void foo(bool x)
>>> {
>>>     if (x)
>>>        scope(exit) writeln("exiting");
>>>     writeln("body");
>>> }
>>
>> Doesn't this do what you want, without changing the language?
>>
>> void foo(bool x) {
>>     scope(exit) {   
>>       if (x) writeln("exiting");
>>     }
>>     // ...
>> }
> 
> Absolutely. but it is currently possible to write the code Don
> wrote, have it compile, and feel that this is completely
> pointless. Which it is.
> 
> That is, if scope(whatever) is the only thing inside a scope,
> something is wrong. D already warns us of other stupid things
> we do, and this is a prime candidate.

More importantly, the error message could suggest the appropriate use 
case (defining a local bool and then swapping if and scope). It's a 
common pattern that may deserve careful treatment.

Andrei


More information about the Digitalmars-d mailing list