On 3/9/11 5:15 PM, bearophile wrote:
Despite D is currently not widely used, it's not hard for me to find references 
about D into computer science papers I find around.

This paper is titles "Code Sandwiches", by Matt Elder, Steve Jackson, and Ben 
Liblit, it discusses D scope guards too (page 7 and several successive pages):
http://pages.cs.wisc.edu/~liblit/tr-1647/

One of the things the paper says about D scope guards is: "Scope guards do not 
provide encapsulation".

Bye,
bearophile

So strange they don't mention Ruby, which has the best code sandwiches ever :-P

Sample code (won't work, this is just the idea)

  def with_lock(some_lock)
    some_lock.lock

    yield

    some_lock.unlock
  end

Usage:

  with_lock(foo) do
    # Whatever
  end

This way you get syntactic linkage and encapsulation, but not inevitability (because # Whatever might raise an exception). For that you have to do:

  def with_lock(some_lock)
    some_lock.lock

    yield
  ensure
    some_lock.unlock
  end

Reply via email to