www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should scope(exit) be valid outside of a scope?

reply Don <nospam nospam.com> writes:
Consider the following code
----------
import std.stdio;

void foo(bool x)
{
    if (x)
       scope(exit) writeln("exiting");
    writeln("body");
}
----------
If the scope(exit) isn't in a compound statement (ie, if it isn't inside 
{}), the 'scope' applies only to the statement itself, so that's 
identical to:
    if (x) writeln("exiting");
which is useless and probably not what was intended.
Ditto for scope(success).

Currently the scope(exit) doesn't get executed at all (it's bugzilla 
1894). But I suspect that any such use of scope guards is a bug.
Can we just make it illegal?
May 11 2010
next sibling parent reply Adam Ruppe <destructionator gmail.com> writes:
On Tue, May 11, 2010 at 10:18:29PM +0200, Don wrote:
 Currently the scope(exit) doesn't get executed at all (it's bugzilla
 1894). But I suspect that any such use of scope guards is a bug.
 Can we just make it illegal?
I was pondering named unit tests and wanted to use this: { if(getenv("VERBOSE_TESTS")) scope(exit) writefln("Test successful"); } But it didn't work. The alternative, scope(exit) if(blah) write; should accomplish the same task though. I'd be good with it being an error, to make it obvious which method is preferred.
May 11 2010
parent reply S <S S.com> writes:
On 2010-05-11 13:38:15 -0700, Adam Ruppe said:

 On Tue, May 11, 2010 at 10:18:29PM +0200, Don wrote:
 Currently the scope(exit) doesn't get executed at all (it's bugzilla
 1894). But I suspect that any such use of scope guards is a bug.
 Can we just make it illegal?
I was pondering named unit tests and wanted to use this: { if(getenv("VERBOSE_TESTS")) scope(exit) writefln("Test successful"); } But it didn't work. The alternative, scope(exit) if(blah) write; should accomplish the same task though. I'd be good with it being an error, to make it obvious which method is preferred.
static if doesn't create a new scope. You should use that instead and a debug version. -S
May 11 2010
parent Adam Ruppe <destructionator gmail.com> writes:
On Tue, May 11, 2010 at 06:56:52PM -0700, S wrote:
 static if doesn't create a new scope.   You should use that instead and
 a debug version.
But you can't static if based on a runtime value.
May 11 2010
prev sibling next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Don wrote:

 If the scope(exit) isn't in a compound statement (ie, if it isn't inside 
 {}), the 'scope' applies only to the statement itself, so that's 
...
 Can we just make it illegal?
I don't see a reason to allow it. I vote error.
May 11 2010
parent reply S <S S.com> writes:
On 2010-05-11 17:58:29 -0700, Jesse Phillips said:

 Don wrote:
 
 If the scope(exit) isn't in a compound statement (ie, if it isn't inside
 {}), the 'scope' applies only to the statement itself, so that's
...
 Can we just make it illegal?
I don't see a reason to allow it. I vote error.
It is inside a scope. Not scope(exit)'s fault that you have only it inside your particular scope (the scope of the if statement). It could produce a warning....
May 11 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
S <S s.com> wrote:

 On 2010-05-11 17:58:29 -0700, Jesse Phillips said:

 Don wrote:

 If the scope(exit) isn't in a compound statement (ie, if it isn't  
 inside
 {}), the 'scope' applies only to the statement itself, so that's
...
 Can we just make it illegal?
I don't see a reason to allow it. I vote error.
It is inside a scope. Not scope(exit)'s fault that you have only it inside your particular scope (the scope of the if statement). It could produce a warning....
"Warnings are not a defined part of the D Programming Language."[1] Scope(whatever) as the only piece of code inside a scope is as useless as wings on an elephant. There might be cases, however, where generated code would create just that, and thus mayhap it should be just a warning. Or, one could make it an error if 'staches[2] are omitted. In this case, generated code should have no problems, and it serves to highlight that even without 'staches, if introduces a scope. [1]: http://www.digitalmars.com/d/2.0/warnings.html [2]: Also known as curly brackets - '{' and '}' -- Simen
May 12 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Simen kjaeraas wrote:

 "Warnings are not a defined part of the D Programming Language."[1]

 Scope(whatever) as the only piece of code inside a scope is as useless
 as wings on an elephant. There might be cases, however, where generated
 code would create just that, and thus mayhap it should be just a warning.
 Or, one could make it an error if 'staches[2] are omitted. In this
 case, generated code should have no problems, and it serves to highlight
 that even without 'staches, if introduces a scope.

 [1]: http://www.digitalmars.com/d/2.0/warnings.html
 [2]: Also known as curly brackets - '{' and '}'
Best I could tell, Don was only looking at making it an error when you leave out {}. I don't support it as an error if {} are surrounding it.
May 12 2010
prev sibling parent reply Graham Fawcett <fawcett uwindsor.ca> writes:
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"); } // ... } Best, Graham
May 12 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Graham Fawcett <fawcett 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. -- Simen
May 12 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Simen kjaeraas wrote:
 Graham Fawcett <fawcett 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
May 12 2010