Idea: break/continue and scope guards

mike vertex at gmx.at
Fri Jul 6 08:28:41 PDT 2007


Hi!

Don't know if that makes any sense or if it would be of any use, but I had an idea lately and wanted to share it here, maybe it's some little thing that could be nice in D.

So here we go:

' foreach (customer; customerList)
' {
'     foo(customer);
'     scope(failure) foo_rollback(customer);
'
'     bool ok = check_valid_after_foo(customer);
'     if (!ok) continue(failure);
'
'     commit(customer);
' }

Basically the idea is to trigger the appropriate scope guards when a break or continue occurs. Another thing where this could be handy is in situations where you loop over an array to search for a specific item and do something when it is found, like:

' foreach (customer; customerList)
' {
'     scope(success) result = customer;
'     if (customer.ID == wantedID) break(success);
' }

What do you think?

-mike



More information about the Digitalmars-d-announce mailing list