DMD 0.148 - scope guard

John Reimer John_member at pathlink.com
Mon Feb 27 01:26:01 PST 2006


In article <dtueau$pdo$1 at digitaldaemon.com>, Cris says...
>
>Now you have just to persuade Walter Bright or to write your own 
>compiler/language :)
>
>Dawid Ciężarkiewicz wrote:
>> Dawid Ciężarkiewicz wrote:
>>> What do you think? Maybe later we'll come with better ideas.
>> 
>> VERSION I: (yeah, I know ...)
>> 
>> h3r3tic on #D channel said that VERSION H is too long and too switch-like.
>> 
>> This is hybrid of two concepts - verbose and informative "register" keyword
>> and short usage without switch-like syntax.
>> 
>> void LongFunction()
>> {
>>     State save = UIElement.GetState();
>>     register (scopepass) UIElement.SetState(save);
>>     register (scopefail) UIElement.SetState(Failed(save));
>>     ...lots of code...
>> }
>> 


Hmm... we have "static if";  why not adopt a "scope if"?

Here's a sample using Walter's Mailer example from
http://www.digitalmars.com/d/exception-safe.html:

# class Mailer
# {
#     void Send( Message msg )
#     {
#          {
#              char[] origTitle = msg.Title();
#              
#              scope if (exit)
#                  msg.SetTitle( origTitle );
#              
#              msg.SetTitle("[Sending] " ~ origTitle);
#	       Copy(msg, "Sent");
#          }
#	   scope if(success) 
#              SetTitle(msg.ID(), "Sent", msg.Title);
#          scope if (failure)
#             Remove(msg.ID(), "Sent");
#          SmtpSend(msg);    // do the least reliable part last
#      }
# }

Simple, clear, and parallel to other D constructs, no?  We've certainly come to
enjoy the "static if" syntax.

-JJR





More information about the Digitalmars-d mailing list