digitalmars.D - Should scope(exit) be valid outside of a scope?
- Don (19/19) May 11 2010 Consider the following code
- Adam Ruppe (10/13) May 11 2010 I was pondering named unit tests and wanted to use this:
- S (4/21) May 11 2010 static if doesn't create a new scope. You should use that instead and
- Adam Ruppe (2/4) May 11 2010 But you can't static if based on a runtime value.
- Jesse Phillips (3/6) May 11 2010 I don't see a reason to allow it. I vote error.
- S (4/14) May 11 2010 It is inside a scope. Not scope(exit)'s fault that you have only it
- Simen kjaeraas (12/25) May 12 2010 "Warnings are not a defined part of the D Programming Language."[1]
- Jesse Phillips (3/12) May 12 2010 Best I could tell, Don was only looking at making it an error when you
- Graham Fawcett (10/16) May 12 2010 Doesn't this do what you want, without changing the language?
- Simen kjaeraas (9/23) May 12 2010 Absolutely. but it is currently possible to write the code Don
- Andrei Alexandrescu (5/32) May 12 2010 More importantly, the error message could suggest the appropriate use
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
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
On 2010-05-11 13:38:15 -0700, Adam Ruppe said:On Tue, May 11, 2010 at 10:18:29PM +0200, Don wrote:static if doesn't create a new scope. You should use that instead and a debug version. -SCurrently 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
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
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
On 2010-05-11 17:58:29 -0700, Jesse Phillips said:Don wrote: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....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
S <S s.com> wrote:On 2010-05-11 17:58:29 -0700, Jesse Phillips said:"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 '}' -- SimenDon wrote: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....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 12 2010
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
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
Graham Fawcett <fawcett uwindsor.ca> wrote:On Tue, 11 May 2010 22:18:29 +0200, Don wrote: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. -- Simenvoid 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"); } // ... }
May 12 2010
Simen kjaeraas wrote:Graham Fawcett <fawcett uwindsor.ca> wrote: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. AndreiOn Tue, 11 May 2010 22:18:29 +0200, Don wrote: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.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"); } // ... }
May 12 2010