On Tuesday, 24 May 2016 at 02:01:14 UTC, cy wrote:
void transaction(Callable)(Callable do_stuff)
                             ^^^^^^^^
                             `scope` to avoid GC
if(isCallable(Callable)) {
  begin();
  scope(failure) rollback();
  scope(success) commit();
  do_stuff();
}

...

transaction({
  big_long_complicated_code_block();
});

This technique is very widely used in Ruby, and I agree that it's really useful. It would be even nicer if we had trailing delegates, as Jacob Carlborg suggested [1]:

db.transaction {
  // ...
}

[1] http://forum.dlang.org/post/modnbn$184o$1...@digitalmars.com

Reply via email to