DMD 0.148 - scope guard

Mike Capp mike.capp at gmail.com
Tue Feb 28 05:22:54 PST 2006


In article <du0slm$lj0$2 at digitaldaemon.com>, Walter Bright says...
>
>Ruby's rescue thing looks like try-catch. on_scope is much more than 
>try-catch, or I wouldn't have implemented it <g>. 

on_scope is quite cute, and certainly cleaner than the C++ implementation of
ScopeGuard. Compared to ctor/dtor RAII, though, it still has the same problem as
try-catch{-finally} - a naive use of a resource is wrong by default, and the
programmer has to do something extra to make it right.

When I first saw Andrei's paper it struck me that what made things painful in
C++ was the lack of usable closures. Since D is pretty good in this regard,
could the same effect have been achieved with a less sugary but more regular
approach reusing RAII-auto syntax, e.g.

# import std.scopeguard;
# auto Guard onOk = new OnScopeSuccessGuard(&mySuccessFunc);
# auto Guard onFail = new OnScopeFailureGuard(function { /* ad hoc code */ });

with something like C++'s std::uncaught_exception in the Guard subclass
destructor implementations to discriminate exit conditions?

cheers
Mike





More information about the Digitalmars-d mailing list