On Wed, 2015-10-28 at 11:00 +0100, Erik van Velzen wrote: > Subject says it all. > > I want to make a small "ScopeGuard" library for exception-safe > transactions, like the following: > > $scope = new ScopeGuard; > > $scope->onSucces(function() { logTransactionOne(); }); > $scope->onFailure(function() { rollbackTransationOne(); }); > doTransactionOne(); > > $scope->onSuccess(function() { logTransactionTwo(); }); > $scope->onFailure(function() { rollbackTransactionTwo(); }); > $scope->onFailure(function() { logFailureOfTransactionTwo(); }); > doTransactionTwo(); > > // ... Three Four etc... > > So if an exception is thrown the attached onFailure callbacks will be > executed, if the scope is exited through a return however the success > callbacks will be executed.
The way to do this is by marking the success in an explicit way. See http://slides.com/johannes4321/froscon15/#/4 and http://slides.com/johannes4321/froscon15/#/6 With your attempt you can only handle abortion via exception. With a pattern like shown in my slides you can also handle cases where you "return" early nicely. johannes
signature.asc
Description: This is a digitally signed message part