why scope(success)?

Mike Capp mike.capp at gmail.com
Sat May 13 07:51:49 PDT 2006


In article <e42jr6$2u07$1 at digitaldaemon.com>, Walter Bright says...
>
>For example, if I have operations A(), B(), and C(), and A.rollback(), 
>B.rollback() and C.rollback:
>
>	A();
>	scope(failure) A.rollback();
>	B();
>	scope(failure) B.rollback();
>	C();
>	scope(failure) C.rollback();
>
>Just for fun, try to do that with either C++ exceptions or Java try-finally.

Off the top of my head...

struct A // ditto for B and C
{
A() { doA(); }
~A() { if (std::uncaught_exception()) { rollbackA(); } }
};

A a;
B b;
C c;

I'm sure there are drawbacks; there always are. It's horses for courses. If the
operations are common ones, the C++ approach wins out IMHO because you don't
have to remember the scope statement (which is still a "wrong by default"
design). If the operation is an ad-hoc one and you have to write the wrapper
class yourself, the D approach is much cleaner since it keeps everything
together and clarifies the intent. (Just look how popular std::for_each isn't.)

cheers
Mike





More information about the Digitalmars-d mailing list