May 10, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | On Wed, 10 May 2006 15:52:36 -0700, Sean Kelly <sean@f4.ca> wrote:
> Derek Parnell wrote:
>> On Thu, 11 May 2006 08:13:37 +1000, Sean Kelly <sean@f4.ca> wrote:
>>
>>> Chris Miller wrote:
>>>> This gives me an idea, how about if there was scope(none) that is just like a regular block, but doesn't create a new scope. This would be for when you only need to group statements but have no interest in a new scope.
>>>> void foo()
>>>> {
>>>> if(a) scope(none) { stuff(); scope(success) bar(); }
>>>> baz();
>>>> }
>>>> bar() would execute after baz();
>>>
>>> It's a nasty hack, but:
>>>
>>> if(!a) goto blah;
>>> scope(success) a.foo();
>>> blah:
>>> ...
>> And I guess this is just as nasty ...
>> scope(success) if (a) a.foo();
>
> Preferable so long as the state of a is maintained until scope exit. But all of this smacks as an attempt to use the scope feature in a manner that wasn't intended.
Yeah. It's a pity though. What about:
int foobar( .. )
{
if (a) {
scope(success) scope(success) a.foo();
}
}
I wonder.
Regan
|
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "Sean Kelly" <sean@f4.ca> wrote in message news:e3toih$26v4$1@digitaldaemon.com... > Chris Miller wrote: >> >> This gives me an idea, how about if there was scope(none) that is just like a regular block, but doesn't create a new scope. This would be for when you only need to group statements but have no interest in a new scope. >> >> void foo() >> { >> if(a) scope(none) { stuff(); scope(success) bar(); } >> baz(); >> } >> >> bar() would execute after baz(); > > It's a nasty hack, but: > > if(!a) goto blah; > scope(success) a.foo(); > blah: > ... In Cx I was going to make that illegal - much like in C++ it's illegal to goto past an initializer or into a try block. > > > Sean |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "Sean Kelly" <sean@f4.ca> wrote in message news:e3tjf6$1v3n$1@digitaldaemon.com... > James Dunne wrote: >> >> if statements do not create a scope without { }, therefore it should be at B. > > They don't? > > > Sean They do. The code if (1) int a = 10; a = 20; is illegal. |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | I've been writing an OO binding to the cairo 2D drawing API, and scope(success) has been a huge help. Basically, cairo's api functions rarely if ever return an error code. Instead, they set a flag on the object's pointer itself[1]_. Since I need to check this flag after every call I make on any particular object, each wrapper class has a protected "checkStatus" member. Then, I just need to write this: void someMember() { scope(success) checkStatus(); // ... do cairo stuff ... } When the "do cairo stuff" is just a simple function call, I don't really gain anything. But it comes in handy when I've got to return values from cairo functions, or when I have to use any kind of complex logic. Plus, I find reading "scope(success) checkStatus()" to be very clear; that, and it's immediately obvious just looking at the code if I've forgotten it (almost every function in the binding follows that pattern so exceptions to that are easily spotted). -- Daniel .. [1]: When I say "object" what I mean is that objects in cairo are just pointers to undefined structs. So a "cairo_pattern_t*" is an "object" :P P.S. Cx looks very cool; must have a poke around that... Ben Hinkle wrote: > I hope this doesn't come of as a flame, but I'm wondering if anyone is using scope(success) and why. I can't find any reason for it. > > Some background: I've slowed my D work to focus on some C experimental > features I'm calling Cx: http://www.tinycx.org and currently I'm > implementing the error handling using reserved labels "error:" and > "finally:". The error label is roughly like scope(failure) and the finally > label is roughly like scope(exit). There's no try-catch-finally. I don't > plan on adding anything like scope(success) because I couldn't think of why > anyone would want to use it. Why not just put the code at the end of the > scope like normal code-flow? I suppose one could code the entire scope in > reverse just for kicks: > void main() { > scope(success) printf("world\n"); > scope(success) printf("hello "); > } > > -Ben > > -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/ |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: > "Sean Kelly" <sean@f4.ca> wrote in message news:e3tjf6$1v3n$1@digitaldaemon.com... > >>James Dunne wrote: >> >>>if statements do not create a scope without { }, therefore it should be at B. >> >>They don't? >> >> >>Sean > > > They do. The code > if (1) > int a = 10; > a = 20; > is illegal. > > Then the implementation according to D's language specs is incorrect. Nothing is mentioned about new scopes created by if-statements or while-statements. New scopes are only created from block { } statements when inside a function body. Scope is mentioned (in passing) for the for-statement; the initializer is noted as a special case. Did I miss a blanket statement somewhere else in the docs about this? -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e h>--->++ r+++ y+++ ------END GEEK CODE BLOCK------ James Dunne |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | James Dunne wrote: <snip> > Then the implementation according to D's language specs is incorrect. Nothing is mentioned about new scopes created by if-statements or while-statements. New scopes are only created from block { } statements when inside a function body. Scope is mentioned (in passing) for the for-statement; the initializer is noted as a special case. Did I miss a blanket statement somewhere else in the docs about this? I think you're meant to use a bit of common sense here. What sense does it make for a declaration to be conditional at runtime? To be honest, I think a naked declaration as the body of a runtime control statement should be illegal. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Miller | Chris Miller wrote: <snip> > void foo() > { > if(a) scope(none) { stuff(); scope(success) bar(); } > baz(); > } > > bar() would execute after baz(); I'm not sure about this. Of course, it would need to be illegal to declare anything within a scope(none) block. Implementing scopeless conditionals would add a runtime dependence to the ScopeStatement mechanism. The program would have to either note the value of the conditional at the time, or use some kind of function stack to keep track of which ScopeStatements will be executed. Moreover, what if it's a loop rather than an if? Should the cleanup code be executed once for each iteration of the loop, all at once when exiting the scope containing the loop? Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:e3vkhn$1kq5$1@digitaldaemon.com... > James Dunne wrote: > <snip> >> Then the implementation according to D's language specs is incorrect. Nothing is mentioned about new scopes created by if-statements or while-statements. New scopes are only created from block { } statements when inside a function body. Scope is mentioned (in passing) for the for-statement; the initializer is noted as a special case. Did I miss a blanket statement somewhere else in the docs about this? > > I think you're meant to use a bit of common sense here. What sense does it make for a declaration to be conditional at runtime? > > To be honest, I think a naked declaration as the body of a runtime control statement should be illegal. > > Stewart. You're probably right. I had tried dmc and cl and they both complained about the use of 'a' without a declaration but in fact I was expecting an error that a declaration can't be the body of an 'if' statement. Since both compiler didn't say boo about the declaration I figured that declarations are considered statements. The C99 spec doesn't consider a declaration a statement so I suspect the compilers are giving poor errors. |
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Hey Ben glad to see you posting again :). tinycx looks cool am trying it out.
I've never used scope(success) , so no comment really ;).
Charlie
Ben Hinkle wrote:
> I hope this doesn't come of as a flame, but I'm wondering if anyone is using scope(success) and why. I can't find any reason for it.
>
> Some background: I've slowed my D work to focus on some C experimental features I'm calling Cx: http://www.tinycx.org and currently I'm implementing the error handling using reserved labels "error:" and "finally:". The error label is roughly like scope(failure) and the finally label is roughly like scope(exit). There's no try-catch-finally. I don't plan on adding anything like scope(success) because I couldn't think of why anyone would want to use it. Why not just put the code at the end of the scope like normal code-flow? I suppose one could code the entire scope in reverse just for kicks:
> void main() {
> scope(success) printf("world\n");
> scope(success) printf("hello ");
> }
>
> -Ben
>
>
|
May 11, 2006 Re: why scope(success)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote:
> "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:e3vkhn$1kq5$1@digitaldaemon.com...
>> James Dunne wrote:
>> <snip>
>>> Then the implementation according to D's language specs is incorrect. Nothing is mentioned about new scopes created by if-statements or while-statements. New scopes are only created from block { } statements when inside a function body. Scope is mentioned (in passing) for the for-statement; the initializer is noted as a special case. Did I miss a blanket statement somewhere else in the docs about this?
>> I think you're meant to use a bit of common sense here. What sense does it make for a declaration to be conditional at runtime?
>>
>> To be honest, I think a naked declaration as the body of a runtime control statement should be illegal.
>
> You're probably right. I had tried dmc and cl and they both complained about the use of 'a' without a declaration but in fact I was expecting an error that a declaration can't be the body of an 'if' statement. Since both compiler didn't say boo about the declaration I figured that declarations are considered statements. The C99 spec doesn't consider a declaration a statement so I suspect the compilers are giving poor errors.
What about this:
if( auto i = doSomething() )
printError( i );
I don't think anyone would expect 'i' to survive after the printError expression.
Sean
|
Copyright © 1999-2021 by the D Language Foundation