On 12/18/06, Blair Sutton <[EMAIL PROTECTED]> wrote:
I agree entirely that not all objects need this capability but some
certainly do. That is, the capability to execute code once every
reference of an object has been removed. Could you point to, or give an
example of the Perl 6 way for doing something similar?

Well, there is one general method I can think of.  You need to know
more about your program in order to implement it; i.e. if an object
which contains an array of objects, one element of which has a socket
that needs closing, it's going to be tough to get it right.  That's
one of the negative sides of the trade-off Larry mentioned (but he
made an excellent case for the positive sides, and the reason why
we're leaning the nondeterministic way).

The main tool at your disposal is the LEAVE closure trait.  i.e.:

   sub foo {
       do_stuff();
       do_more_stuff();
       LEAVE { say "leaving foo" }
   }

This code will say "leaving foo" whenever its dynamic scope ends,
whether that time is the successful completion of do_more_stuff or
whether do_stuff threw an exception.  If I understand the idiom
correctly, this exceptional case was the main reason behind RAII.  Do
your cleanup in the LEAVE block.

If you have a specific scenario that comes up a lot, other than "the
exact semantics of RAII", do describe it and that will give us
something to think about.  Either there's already some clever trick
you can do to handle it, there's a little extra language feature we'll
have to add, or you'll have to put up with doing a little more manual
bookkeeping.

Luke

Reply via email to