scope(exit) considered FREAKING AWESOME

downs default_357-line at yahoo.de
Sun Nov 8 13:15:14 PST 2009


Justin Johansson wrote:
> downs Wrote:
> 
>> auto var = hairilyAllocatedObject();
>> scope(exit) cleanupObject(var);
>>
>> sock.send("<html><head></head><body>");
>> scope(exit) sock.send("</body></html>");
>>
>> logln("Digraph D {");
>> scope(exit) logln("}");
>>
>> SDL_LockSurface(display.surface);
>> scope(exit) SDL_UnlockSurface(display.surface);
>>
>> auto dg = { performComplexExitCleanupDuty; };
>> scope(exit) dg();
>> ....
>> dg = dg /apply/ (proc dg) { moreCleanup; dg(); };
>> ....
>> /* Half a dozen different ways of leaving the function go here */
>>
>> auto backup = var; var = newValue;
>> scope(exit) var = backup;
> 
> Yes fine.  You demonstrate the cool use of scope(exit) but
> this is a bit like kidding yourself and conscientiously
> thinking you didn't *actually* tell a lie because you just
> withheld information.
> 
> Conspicuous, by the absence of, in your examples is any
> use of scope(exit) inside curly-brace scope statements
> embedded *inside* a function.
> 
> Your examples look fine if one assumes scope(exit) as
> relating to function scope.
> 

I assume scope(exit) is relating to whatever scope I'm currently in. I'm not sure how my examples oppose that.

When I do scope(exit) in a while loop, I expect it to be run at the end of the loop, not at the end of the function.

Don't blame the language for your misunderstanding of it.



More information about the Digitalmars-d mailing list